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-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to