[PYTHON] Django model: ManyToManyField

Model example and definition

Consider a soccer player (Player) and its position (Position) as an example.

  1. Players can have multiple positions
  2. There are multiple players in the same position

In this case, ManyToMany comes into play.

Model example


class Position(models.Model):
    id = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=20, unique=True)


class Player(models.Model):
... (Omitted) ...
    position = models.ManyToManyField(Position)

If you migrate Django using the above model definition, the following three tables will be created. image.png

The table called player_position is the PK of the player table and the PK of the position table, and the information is managed as follows. image.png

Search example

Extract all positions of the player "Pirlo"

positions = Player.objects.get(name='Pirlo').position.all()
[print(p.name) for p in positions]

Execution result:
DM
CM

Extract all players in the position of "CB" (Method 1)

players = Player.objects.filter(position=Position.objects.get(name='CB'))
[print(p.name) for p in players]

Execution result:
Maldini
Nesta

Extract all players in the position of "CB" (Method 2)

Model example (method 2)


class Player(models.Model):
... (Omitted) ...
    position = models.ManyToManyField(Position, related_name='player')
players = Position.objects.get(name='CB').player.all()
[print(p.name) for p in players]

Execution result:
Maldini
Nesta

Postscript

Method 1 and Method 2 extracted all the players in the position of "CB", but when I checked the created query, both generated the following SQL.

SELECT "soccer_player"."id", "soccer_player"."name", "soccer_player"."team_id"
FROM "soccer_player"
INNER JOIN "soccer_player_position" ON ("soccer_player"."id" = "soccer_player_position"."player_id")
WHERE "soccer_player_position"."position_id" = 1

Recommended Posts

Django model: ManyToManyField
Model changes in Django
High Performance Django --Model
Django Model Field Customization Various
Django
Use django model from interpreter
Show Django ManyToManyField in Template
Create an easy-to-use follow model in Django using ManyToManyField through
Django Model with left outer join
Automatically generate model relationships with Django
django update
Django note 4
Django: Migration doesn't reflect model in DB
WEB application development using Django [Model definition]
Django memorandum
django search
Django Summary
Django test
Create a model for your Django schedule
Django # 2 (template)
Django Note 5
Django hands-on
Touch django
django notes
Django Summary
Django basics
Django Shoho
Django defaults
Django + Docker
Get a reference model using Django Serializer
Django Glossary
Django search
Install Django
Django: References
Django Note 1
Django note 3
Django note 2
Implement a Custom User Model in Django
Django startup
Django notes
Django NullCharField
Django Tutorial Summary for Beginners by Beginners (Model, Admin)
[django] model mods may or may not be updated