List Info

Thread: FileField and form.save()




FileField and form.save()
user name
2007-12-18 11:08:41
I am trying to allow certain users to upload media and then
"attach"
it via the contenttypes framework. The code posted below
just puts the
contents of the file in the media field. How do I make this
work with
the model.FileField? (putting it in the
settings.MEDIA_ROOT+upload_to
directory?) This is the code

I have the following model:

class Media(models.Model):
    SCOPE_CHOICES = (
        ('PUB', 'Public'),
        ('ALL', 'All users'),
        ('TEA', 'All teachers'),
        ('STU', 'All students'),
        ('STC', 'Only Students in class'),
    )
    name = models.CharField(max_length=96)
    media = models.FileField(upload_to='upload/%y/%m/%d')
    uploader = models.ForeignKey(User)
    scope = models.CharField(max_length=3,
choices=SCOPE_CHOICES)
    accessed = models.PositiveIntegerField()
    description = models.TextField()

and this form:

class MediaUploadForm(forms.Form):
    media = forms.FileField()
    description = forms.CharField(max_length=1024,
widget=forms.Textarea)
    sp_id = forms.CharField(max_length=32,
widget=forms.HiddenInput)

    def clean_sp_id(self):
        sp_id = int(self.cleaned_data['sp_id'])
        try:
            self.scheduled_product =
ScheduledProduct.objects.get(id=sp_id)
        except ObjectDoesNotExist:
            raise forms.ValidationError('Invalid scheduled
product.')
        return self.cleaned_data['sp_id']

    def save(self, user):
        if user != self.scheduled_product.teacher:
            return False
        media =
Media(name=self.cleaned_data['media'].filename,
                     
media=self.cleaned_data['media'].content,
                      uploader=user, scope='STC',
accessed=0,
                     
description=self.cleaned_data['description'])
        media.save()

        content_type =
ContentType.objects.get_for_model(ScheduledProduct)
        content_media =
ContentMedia(content_type=content_type,
 
object_id=self.scheduled_product.id,
                                     media=media)
        content_media.save()

        return True
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: FileField and form.save()
user name
2007-12-18 11:14:16
Just to make the question a little more clear:

What type of object does model.FileType expect to get?

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: FileField and form.save()
user name
2007-12-18 11:52:58
On Dec 18, 2007 12:08 PM, Ryan K <ryankaskelgmail.com> wrote:
>     def save(self, user):
>         if user != self.scheduled_product.teacher:
>             return False
>         media =
Media(name=self.cleaned_data['media'].filename,
>                      
media=self.cleaned_data['media'].content,
>                       uploader=user, scope='STC',
accessed=0,
>                      
description=self.cleaned_data['description'])
>         media.save()

You don't pass in the contents of the file to the model's
constructor
like this. Instead, you instantiate the object, *then* save
the file.
Saving a file also saves the object, by default, so you can
do it all
in one pass. Try something like this instead:

    def save(self, user):
        if user!= self.schedule_product.teacher:
            return False
        media =
Media(name=self.cleaned_data['media'].filename,
                      uploader=user, scope='STC',
accessed=0,
                     
description=self.cleaned_data['description'])
       
media.save_media_file(self.cleaned_data['media'].filename,
self.cleaned_data['media'].content)

Here, media.save_media_file() will actually write the file
where it
needs to go, update the model to reflect that location, and
save the
record to the database, all at once.

-Gul

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: FileField and form.save()
user name
2007-12-18 13:03:59
Thanks a lot. Missed save_FOO_file in the docs.

On Dec 18, 12:52 pm, "Marty Alchin"
<gulop...gamemusic.org> wrote:
> On Dec 18, 2007 12:08 PM, Ryan K <ryankas...gmail.com> wrote:
>
> >     def save(self, user):
> >         if user !=
self.scheduled_product.teacher:
> >             return False
> >         media =
Media(name=self.cleaned_data['media'].filename,
> >                      
media=self.cleaned_data['media'].content,
> >                       uploader=user, scope='STC',
accessed=0,
> >                      
description=self.cleaned_data['description'])
> >         media.save()
>
> You don't pass in the contents of the file to the
model's constructor
> like this. Instead, you instantiate the object, *then*
save the file.
> Saving a file also saves the object, by default, so you
can do it all
> in one pass. Try something like this instead:
>
>     def save(self, user):
>         if user!= self.schedule_product.teacher:
>             return False
>         media =
Media(name=self.cleaned_data['media'].filename,
>                       uploader=user, scope='STC',
accessed=0,
>                      
description=self.cleaned_data['description'])
>        
media.save_media_file(self.cleaned_data['media'].filename,
> self.cleaned_data['media'].content)
>
> Here, media.save_media_file() will actually write the
file where it
> needs to go, update the model to reflect that location,
and save the
> record to the database, all at once.
>
> -Gul
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )