擅长:python、mysql、java
<p>您需要向模型中添加一个元类</p>
<p>你的学生模型应该是这样的:</p>
<pre><code> class Student(models.Model):
class Meta:
unique_together = ("grade", "rollno")
name = models.CharField(max_length=255)
grade = models.ForeignKey(grade, on_delete=models.CASCADE)
rollno = models.BigIntegerField()
</code></pre>
<p>关于django模型的元选项的文档在这里:<a href="https://docs.djangoproject.com/en/2.1/ref/models/options/#unique-together" rel="nofollow noreferrer">https://docs.djangoproject.com/en/2.1/ref/models/options/#unique-together</a></p>
<p>它们真的很有用</p>
<p>顺便说一句,在变量名中使用下划线和全字是惯例。您应该考虑将<code>rollno</code>更改为<code>roll_number</code></p>