gpt4 book ai didi

python - 在 Django 模型中的定义之前使用一个类

转载 作者:太空狗 更新时间:2023-10-30 01:47:02 24 4
gpt4 key购买 nike

当我尝试 syncdb 时,出现错误 Menu is not a valid class Name。

我怎样才能解决这个关系案例:

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)

最佳答案

来自 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:

例如:

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

编辑:
作为Francis mentions (如 the documentation 中所写):

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

关于python - 在 Django 模型中的定义之前使用一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9606624/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com