Home > other >  How to realize the django many-to-many relationship model, the query of the queryset more terms?
How to realize the django many-to-many relationship model, the query of the queryset more terms?

Time:10-09

Query films and film star, and the star is a many-to-many relationship, namely each film can have a number of star of stars, each star can participate in many movies,
problem: how to use queryset query at the same time have more than one star cast of the film?
Data model is as follows:
The class Movies (models. Model) :
Number # movie
M_id=models. AutoField (primary_key=True)
# movie name
Title=models. TextField (blank=True, null=True)
# other properties



# movie starClass Stars (models. Model) :
# star number
S_id=models. AutoField (primary_key=True)
# star name
Name=models. TextField (blank=True, null=True)
# other properties

# and film star many-to-many relationship table
The class (MS models. Model) :
# keys
M_s_id=models. AutoField (primary_key=True)
# film off-balance-sheet key foreign keys
M_id=models. ForeignKey (to='Movies',
To_field='m_id,
On_delete=models. DO_NOTHING,
Related_name='t_ms',
Blank=True,
Null=True)
# star off-balance-sheet key
S_id=models. ForeignKey (to='Stars',
To_field='s_id,
On_delete=models. DO_NOTHING,
Related_name='t_ms',
Blank=True,
Null=True)

Sample data, for example:


problem: how to use queryset query at the same time have more than one star cast of the film? For example, Robert DE niro and al pacino in the movie?
  • Related