It doesn't take many, many relationships giving a ValueError

I am having a problem where I have an optional I-with-many relationship which when saving a new object to an empty psql db gives me:

Edit: this is when I save admin, there is no view that saves the model.

ValueError: "Video: Teste" must have a value for the "from_video" field before using this many-to-many relationship.

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 does the optional field give me a ValueError when nothing is added to the many-to-many field? And how can I fix this?

+3


source to share





All Articles