4

I'm having an issue where I have a non required self many-to-many relationship that when saving a new object to an empty psql db gives me:

Edit: This is when I'm admin saving, there is no view that saves the model.

ValueError: "Video: Teste" needs to have a value for field "from_video" before this many-to-many relationship can be used.

This is my model:

class Video(models.Model):
    title = models.CharField(max_length=200, unique=True)
    subtitle = models.CharField(max_length=400)
    thumbnail = models.ImageField(upload_to='videos/thumbnails')
    related_videos = models.ManyToManyField('self', symmetrical=False, blank=True)

This is my save function:

def save(self, *args, **kwargs):
   if self.id is None:
     # Elasticsearch document creation if word does not exist
            video = VideoDocType(title=self.title, subtitle=self.subtitle, thumbnail=str(self.thumbnail))                                   

            video.save()
   else:
       old_value = Video.objects.get(id=self.id)

       thumbnail_url = str(self.thumbnail)

       video = self._get_video(self)

       if video is None:
                video = VideoDocType(title=self.title, subtitle=self.subtitle, thumbnail=str(self.thumbnail))

                video.save()
            else:
                if old_value.thumbnail != self.thumbnail:
                    thumbnail_url = ("videos/thumbnails/" + thumbnail_url)

                video.update(title=self.title, subtitle=self.subtitle, thumbnail=str(self.thumbnail))

                super(Video, self).save(*args, **kwargs)

My question is, why a non required field gives me the ValueError when there is nothing to be added on the many-to-many field? And how could I fix this?

EVolpert
  • 41
  • 3
  • 2
    add `null=True` to `related_videos` `blank` just means that it won't be invalid if missing from a form. – The_Cthulhu_Kid Aug 01 '17 at 20:07
  • Many-to-many are not affect by Null=True according to this: https://stackoverflow.com/questions/18243039/migrating-manytomanyfield-to-null-true-blank-true-isnt-recognized – EVolpert Aug 01 '17 at 20:13
  • Have you tried adding some `print` statements? Maybe before and after `self._get_video`. – The_Cthulhu_Kid Aug 01 '17 at 21:04
  • What is VideoDocType – iklinac Aug 01 '17 at 22:28
  • 1
    It specifies the field that no information has been provided for: `from_video`. Have you changed your model and forgotten to `makemigration`->`migrate`? – almost a beginner Aug 02 '17 at 01:12
  • @The_Cthulhu_Kid Yes, the error is not even getting into that part, that is when updating a video because of the elasticsearch save, it is giving me this error on the If part of the statement – EVolpert Aug 02 '17 at 13:35
  • @iklinac VideoDocType is the object that I create for saving into elastic search as well, but it does not use related videos, just the other fields of the model – EVolpert Aug 02 '17 at 13:35
  • @almostabeginner I did makemigrations and migrate. The behaviour is almost like it is expecting at least 1 related video, but I want 0 or N instances of videos being related. – EVolpert Aug 02 '17 at 13:35
  • what is the field `from_video`? what model is that field in? I don't see it in your Video model, so where is it? – almost a beginner Aug 03 '17 at 04:21

0 Answers0