In my application there is many-to-one relation, such as one teacher can teach more than one subject. So in admin panel I can't simulate it. Can't add more than one subject:
Here are my codes:
models.py:
class Subject(models.Model):
def __str__(self):
return self.name
name = models.CharField(max_length=200)
credit = models.IntegerField()
class Teacher(models.Model):
def __str__(self):
return self.name
name = models.CharField(max_length=100)
email = models.CharField(max_length=100, null=True)
chair = models.ForeignKey(Chair, on_delete=models.CASCADE)
academic_degree = models.CharField(max_length=100)
subject = models.ForeignKey(Subject, on_delete=models.CASCADE)
Django has extremely great support for a attribute related to many other attribute. In your case a single subject could be taught by multiple teacher and multiple teacher can teach single subject. In order to define relationship like above you can leverage ManyToMany relation(this facilate many subject getting associated with single teacher). In case you need one to Many relationship you can use ManyToOne relation.
subject = models.ManyToManyField(subject)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments