Let users either upload a file or provide an URL to a file

51 views
Skip to first unread message

Tony

unread,
Jan 17, 2018, 9:53:50 AM1/17/18
to Django users

I would like to let users either upload a video file(to AWS S3) or provide an URL to a video, e.g. Youtube/Vimeo.


I found a similar question for Rails: Rails: upload a file OR store a url


But how do I do that in Django(1.11)?


Should I create 2 separate models, or model inheritance with abstract model, let users choose what they want to do in the frontend, then display the appropriate form?

class VideoModel(models.Model):
    title = models.CharField(max_length=200)
    post_by = models.OneToOneField(settings.AUTH_USER_MODEL)
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)

        class Meta:
            abstract = True

class VideoFile(VideoModel):
    video = models.FileField(upload_to='uploads/')

class Videolink(VideoModel):
    URL = URLField(max_length=200)

Would it be better if there is only 1 model with both a FileField and URLField. They are both set to blank = true. Put a message on the page, saying either upload a file or provide a Youtube link. In the backend, check the request.POST whether one of these fields is filled in, if not, render the form again with an error message.


Which one is better to handle such situation? Or they are equally horrible? I couldn't think of a third option.

Matemática A3K

unread,
Jan 17, 2018, 10:48:24 AM1/17/18
to django...@googlegroups.com
On Wed, Jan 17, 2018 at 11:53 AM, Tony <bitbith...@gmail.com> wrote:

I would like to let users either upload a video file(to AWS S3) or provide an URL to a video, e.g. Youtube/Vimeo.


I found a similar question for Rails: Rails: upload a file OR store a url


But how do I do that in Django(1.11)?


Should I create 2 separate models, or model inheritance with abstract model, let users choose what they want to do in the frontend, then display the appropriate form?

class VideoModel(models.Model):
    title = models.CharField(max_length=200)
    post_by = models.OneToOneField(settings.AUTH_USER_MODEL)
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)

        class Meta:
            abstract = True

class VideoFile(VideoModel):
    video = models.FileField(upload_to='uploads/')

class Videolink(VideoModel):
    URL = URLField(max_length=200)
 

Would it be better if there is only 1 model with both a FileField and URLField.

IMO, yes, indeed

They are both set to blank = true. Put a message on the page, saying either upload a file or provide a Youtube link. In the backend, check the request.POST whether one of these fields is filled in, if not, render the form again with an error message.

class VideoModel(models.Model):

    title = models.CharField(max_length=200)
    post_by = models.OneToOneField(settings.AUTH_USER_MODEL)
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)
    video = models.FileField(upload_to='uploads/', blank=True, null=True)
    url = URLField(max_length=200, blank=True, null=True)

    def clean(self):
        super().clean()
        if not self.url and not self.video:
             raise(ValidationError({"url": "Both url and video can't be null"})
     def get_video_url(self):
         return(url if self.url else self.video.url)
 
 This is a way of doing it. If you use a ModelForm, then it will show the error message automatically. You can add some javascript for showing only the one that was chosen. You should have for convenience a function or method that return the url for the video independently of the source.


Which one is better to handle such situation? Or they are equally horrible? I couldn't think of a third option.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/31186b81-e108-47f9-8594-a140dcad3097%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Tony

unread,
Jan 17, 2018, 5:08:11 PM1/17/18
to Django users
Thank you so much. I will give it a go.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages