I have a many-to-many
field called paper
in a model called Issues
. Each Issues
record shall have a list of papers. But the paper
should be unique across the issue
table.
In other words, a paper
that is added once to Issues
should not be able to be added in any other record of the Issues
table.
How do i achieve it?
class Papers(models.Model):
'''
All the published papers
'''
title = models.CharField(max_length=300) # title
def __str__(self):
return self.title
class Issues(models.Model):
'''
All issues
'''
number = models.SmallIntegerField() # issue number
paper = models.ManyToManyField('Papers')
def __str__(self):
return str(self.number)
CodePudding user response:
You just need a foreign key relation for that use case
class Papers(models.Model):
'''
All the published papers
'''
title = models.CharField(max_length=300) # title
issue = models.ForeignKey(Issues, on_delete=models.CASCADE)
def __str__(self):
return self.title
class Issues(models.Model):
'''
All issues
'''
number = models.SmallIntegerField() # issue number
def __str__(self):
return str(self.number)