Django - How to create many to one relation

srvqaqa

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: Screenshot

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)
prashant

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.

edited at
0

Comments

0 comments
Login to comment

Related

MySQL How to create one to Zero or Many relation

pandas how to create a one to many relation (add quantity column)

How to create a one to many relation between models in Ruby on Rails?

How to provide a queryset field for a subclass of a one-to-many relation in Django?

how to serialize two classes have one to many relation in django

How to use one to many relation?

Create tables with Many-to-One relation in Postgresql

Django, haystack, elastic search and one to many relation

Forming Django queryset for one-to-many relation

Many to One Relation in Django With Existing Objects

One-To-Many relation with a specific order in Django

Django many-to-one relation with 3 tables

How to implement Many to many relation in django when the relation contain attributes?

ASP .Net MVC 5 How to create list in Create View? One to many relation in EntityFramework

Django ORM join many to many relation in one query

How to use mappedBy correctly in one to many relation

how to make a row unique in one to many relation

How to use one to many relation in Laravel?

how to insert one to many relation in laravel?

How to achieve one to many relation in dynamodb?

how to select child in one to many relation in jpa

How create relation a user with many favorite things?

How to create a Python Dataframe with columns of dynamic length with one-to-many relation to the first column

How to serialize a one to many relation in django-rest using Model serializer?

How can I list all answers from answer model to my question model in Django ? Relation many to one?

How to create model with relationship one-to-many in Django?

How to create relation table with one table to one column relation

How to have a one to one, and a many to one, relation ship EF?

How to include the 'many' part of a relation from the 'one of many' model?