I have a model named Music and it has artist ManyToManyField. I want to validate that artist and name fields are unique_together. Django will not let to put ManyToManyField into the unique_together list of Meta class. Below is what I am doing and I is giving me an error
<"Music: Hello> needs to have a value for field "id" before this many-to-many relationship can be used.
class Music(models.Model):
name = models.CharField(max_length=256)
artist = models.ManyToManyField(Performer, related_name='all_songs')
file = models.FileField(upload_to='music/')
def save(self, *args, **kwargs):
similar_musics = self.__class__.objects.filter(name=self.name)
for music in similar_musics:
artists = music.artist.all()
diff = set(artists.all()).difference(set(self.artist.all()))
if diff:
raise ValidationError('This song seems to be already created!')
super(Music, self).save(*args, **kwargs)
What is the easiest way to validate so that artist and name fields unique together ?
Additionally, I want to compress music file and stream my compressed music to browser and in the browser uncompress received parts and play. So, it may be similar to spotify but very simple version. What may be best way to do this task? Thank you in advance