我有以下 2 个模型,我想将它们与 ManyToMany 关系链接:
www/hruser/models.py
from django.db import models class HRuser(models.Model): """Custom user class.""" email = models.EmailField(max_length=60, unique=True, db_index=True) is_active = models.BooleanField(default=True) is_staff = models.BooleanField(default=False) video = models.ManyToManyField('www.pandastream.models.Video', related_name='users')
www/pandastream/models.py
from django.db import models from www.hruser.models import HRuser class Video(models.Model): """Video object, really basic for now.""" video_id = models.CharField(max_length=32, unique=True, db_index=True) user = models.ForeignKey(HRuser, related_name='videos')
如您所见,它们位于不同的应用程序中,并且 Video 也有一个指向 HRuser 的 ForeignKey。为了避免循环导入,www/hruser/models.py
我尝试使用文档中定义的惰性关系,但它会在 syncdb 上引发错误:
Error: One or more models did not validate:
hruser.hruser: 'video' has an m2m relation with model www.pandastream.models.Video, which has either not been installed or is abstract.
到目前为止,我已经尝试过:
- 在 python shell 中导入我的视频模型,它可以工作
- 从 mysql(5.6.10) 切换到 sqlite(3.7.12)
- 从 Django 1.5 切换到 Django 1.4
- 将
HRuser.video
字段更改为简单的 ForeignKey 字段 - 看源头
django.core.management.validation
所有这些都没有改变我的问题,所以要么我没有正确理解文档,要么文档有误,但无论哪种方式,任何帮助都将不胜感激。