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?