A
A
albertalexandrov2018-02-20 20:23:56
Django
albertalexandrov, 2018-02-20 20:23:56

How to link two models and save?

Hello!
There are "Competition" and "Team" models:

models.py 

class Competition(models.Model):
    name = models.CharField(max_length=30)

class Team(models.Model):
    myteam_name = models.CharField(max_length=30)

Teams (instances of team1, team2, etc.) can take part in competitions (instances of competition1, competition2, etc.).
How can information about these links be stored (for example, the information that team1 takes part in competition2)? I suppose an intermediate model is needed, for example, Membership, as in the example https://djbook.ru/rel1.9/topics/db/models.html#ext... Or are there any other ways?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2018-02-20
@albertalexandrov

In a simple way , you can store information about these relationships:

class Competition(models.Model):
    name = models.CharField(max_length=30)
    teams = models.ManyToManyField(Team)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question