On Wed, Jan 17, 2018 at 11:53 AM, Tony <bitbithksand...@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
> <https://stackoverflow.com/questions/13547724/rails-upload-a-file-or-store-a-url>
>
>
>
> <https://stackoverflow.com/questions/13547724/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+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@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
> <https://groups.google.com/d/msgid/django-users/31186b81-e108-47f9-8594-a140dcad3097%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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/CA%2BFDnhJuyZGN9_nSbmOoZcknxA3r8LFFRPEx3QQQj9bwPx8hZA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to