Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have two models reference each other Django

I have the following code:

class Game(models.Model):
    title = models.CharField(max_length=50)
    summery = models.CharField(max_length=500)
    key = models.IntegerField()
    pin = models.CharField(max_length=12)
    complete = models.BooleanField()
    invite_sent = models.DateTimeField()
    on = models.ForeignKey(Member, blank = True) #<----


class Member(models.Model):
    email = models.CharField(max_length=100)
    color = models.CharField(max_length=11)
    game = models.ForeignKey(Game) #<----

The "on" foreign key links to one of the members (whose turn it is). All members of a game have their "game" foreign key set to the game they are on. The problem is that Django won't let me reference a class before it has been declared, and since I can't declare them simultaneously...

Edit: To clear things up, here's an example. If there were five members playing one game, all five would have foreign keys to the game. The game on the other hand would have one foreign key to the particular member whose turn it was.

like image 927
sinθ Avatar asked Dec 20 '12 18:12

sinθ


People also ask

What is Relatedmanager Django?

A “related manager” is a manager used in a one-to-many or many-to-many related context. This happens in two cases: The “other side” of a ForeignKey relation. That is: from django.db import models class Blog(models.

How do you represent one to many relationship in Django?

To handle One-To-Many relationships in Django you need to use ForeignKey . The current structure in your example allows each Dude to have one number, and each number to belong to multiple Dudes (same with Business).

How do you inherit models in Django?

Models inheritance works the same way as normal Python class inheritance works, the only difference is, whether we want the parent models to have their own table in the database or not. When the parent model tables are not created as tables it just acts as a container for common fields and methods.

What does On_delete do in Django models?

Django is famous for its robust relational management database management system. The on_delete handle is used to handle the deletion of reference data to maintain the database integrity.


2 Answers

The Django documentation for the ForeignKey field states:

If you need to create a relationship on a model that has not yet been defined, you can use the name of the model, rather than the model object itself.

So in your case, that would be:

class Game(models.Model):
    # Other fields...
    on = models.ForeignKey('Member', blank = True)

class Member(models.Model):
    # Other fields...
    game = models.ForeignKey(Game)
like image 69
Blair Avatar answered Oct 03 '22 14:10

Blair


You don't need to have the two models reference each other with foreign keys. Remove the line:

on = models.ForeignKey(Member, blank = True) #<----

and logically your Member's will still be associated to different Game's (and this makes more sense because a member can belong to one game at a time, whereas a game can have more than one member).

You can use reverse relation to figure out which members are on a particular game.

like image 42
sampson-chen Avatar answered Oct 03 '22 13:10

sampson-chen