Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use a class before its definition in Django model

Tags:

python

django

When I try to syncdb I get the error Menu is not a valid class Name.

How can I resolve that relationship case :

class MenuItem(model.Models)
    title = models.CharField(max_length=200)
    submenus = models.ManyToManyField(Menu, blank=True, null=True)

class Menu(Container):
    links = models.ManyToManyField(MenuItem)
like image 801
Christophe Debove Avatar asked Mar 07 '12 17:03

Christophe Debove


1 Answers

From the Django book:

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:

E.g.:

class MenuItem(model.Models)
    title = models.CharField(max_length=200)
    submenus = models.ManyToManyField('Menu', blank=True, null=True)
                                      ^    ^

Edit:
As Francis mentions (and as is written in the documentation):

It doesn't matter which model has the ManyToManyField, but you should only put it in one of the models -- not both.

like image 100
mechanical_meat Avatar answered Sep 16 '22 12:09

mechanical_meat