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.
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.
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)
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.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.