Django多个多对多关系不起作用

时间:2017-02-28 02:16:50

标签: mysql django python-3.x

单个班级中可能存在多个多对多关系吗?这是我的想法:

政策可以翻译成多种语言
语言可以列出许多政策

一个国家/地区可以列出许多政策(即使是多种语言) 可以在许多国家讨论政策

我正在使用django,python3和mysql,我正在圈子里。许多关系似乎都是问题所在。

许多人通过桌子不工作:         languages = models.ManyToManyField(语言,通过=" Policy_Detail")

循环错误消息的步骤:

别名p3 =" python3"

p3 manage.py迁移
错误消息:django.db.utils.OperationalError:(1050,"表' policytracker_country'已经存在")

删除了policytracker_country和所有其他策略跟踪表

键入p3 manage.py进行迁移 未检测到任何更改

输入p3 manage.py migrate
(1051,"未知表' PolicyDB.policytracker_flag'")

的MySQL>显示表;
+ ------------------------------ +
| Tables_in_PolicyDB |
+ ------------------------------ +
| policytracker_country |
| policytracker_events |
| policytracker_policy_user |
| policytracker_website |
| policytracker_website_filter |
+ ------------------------------ +

使用MySql添加了policytracker_flag和policytracker_label_links (即使我在代码中没有提到任何一个) 删除了所有其他policytracker表

+ ---------------------------- +
| Tables_in_PolicyDB |
+ ---------------------------- +
| policytracker_flag |
| policytracker_label_links |
+ ---------------------------- +

p3 manage.py makemigrations
未检测到任何更改

p3 manage.py migrate

django.db.utils.ProgrammingError:(1146,"表' PolicyDB.policytracker_policy_detail'不存在")
+ ------------------------------ +
| Tables_in_PolicyDB |
+ ------------------------------ +
| auth_group |
| auth_group_permissions |
| auth_permission |
| auth_user |
| auth_user_groups |
| auth_user_user_permissions |
| django_admin_log |
| django_content_type |
| django_migrations |
| policytracker_country |
| policytracker_events |
| policytracker_policy_user |
| policytracker_website |
| policytracker_website_filter |
+ ------------------------------ +

已移动' policytracker.policy_detail'在策略跟踪之前的模型类' policytracker.policy'类
NameError:name' Policy'未定义

将其移回了 p3 manage.py makemigrations
未检测到任何变化

p3 manage.py迁移
"表' policytracker_country'已存在"
......我们已经完整了 - 请参阅第一条错误消息

我做错了什么?这是模特:

class Website_Filter(models.Model): ...

    class Language(models.Model):
        iso_language                    = models.CharField(max_length = 2, primary_key=True)
        flag_image_filename             = models.CharField(max_length = 10, default='en.png')
        nav_section_policy_list_title   = models.CharField(max_length = 30, default='Policies')
        ...

    class Country(models.Model):
        language_country_code           = models.CharField(max_length = 5, primary_key=True, default="en/ca")
        country_name                    = models.CharField(max_length = 50)

    class Policy(models.Model):
        image_filename  = models.CharField(max_length = 30)
        start_date      = models.DateField(default = None)  
        languages       = models.ManyToManyField(Language, through="Policy_Detail")
        countries       = models.ManyToManyField(Country)

    class Policy_Detail(models.Model):
        policy            = models.ForeignKey(Policy)
        language          = models.ForeignKey(Language)   
        needs_translation = models.BooleanField(default = True)
        name              = models.CharField(max_length = 50)
        description1      = models.TextField()
        description2      = models.TextField()
        description3      = models.TextField()
        twitter_handle    = models.CharField(max_length = 30, default='')
        subreddit         = models.CharField(max_length = 30, default='')

    class Website(models.Model):
        name            = models.CharField(max_length = 20, unique=True)
        ip_address      = models.GenericIPAddressField()
        link            = models.CharField(max_length = 50)
        is_pending      = models.BooleanField()
        is_allowed      = models.BooleanField()                

    class Official_Site(Website):
        policy = models.ForeignKey(Policy_Detail)

    class Interviews(Website):
        policy = models.ForeignKey(Policy_Detail)

    class Frequently_Asked_Questions(Website):
        policy = models.ForeignKey(Policy_Detail)

    class Articles(Website):
        policy = models.ForeignKey(Policy_Detail)

    class Books(Website):
        policy = models.ForeignKey(Policy_Detail)

    class Petitions(Website):
        policy = models.ForeignKey(Policy_Detail)

    class Actions(Website):
        policy = models.ForeignKey(Policy_Detail)

    class Rallies(Website):
        policy = models.ForeignKey(Policy_Detail)

    class Events(models.Model):
        policy = models.ForeignKey(Policy_Detail)
        event_date      = models.DateField(null=True)
        ...

    class Policy_User(models.Model):
        country           = models.ForeignKey(Country)
        username          = models.CharField(max_length = 30, default="Anonymous")

    class Vote(models.Model):
        policy            = models.ForeignKey(Policy)
        p_user            = models.ForeignKey(Policy_User)
        voting_date       = models.DateField(auto_now_add = True)
        week_number       = models.IntegerField(default = 1)
        vote_code         = models.IntegerField(default = 1)

class Language(models.Model): iso_language = models.CharField(max_length = 2, primary_key=True) flag_image_filename = models.CharField(max_length = 10, default='en.png') nav_section_policy_list_title = models.CharField(max_length = 30, default='Policies') ... class Country(models.Model): language_country_code = models.CharField(max_length = 5, primary_key=True, default="en/ca") country_name = models.CharField(max_length = 50) class Policy(models.Model): image_filename = models.CharField(max_length = 30) start_date = models.DateField(default = None) languages = models.ManyToManyField(Language, through="Policy_Detail") countries = models.ManyToManyField(Country) class Policy_Detail(models.Model): policy = models.ForeignKey(Policy) language = models.ForeignKey(Language) needs_translation = models.BooleanField(default = True) name = models.CharField(max_length = 50) description1 = models.TextField() description2 = models.TextField() description3 = models.TextField() twitter_handle = models.CharField(max_length = 30, default='') subreddit = models.CharField(max_length = 30, default='') class Website(models.Model): name = models.CharField(max_length = 20, unique=True) ip_address = models.GenericIPAddressField() link = models.CharField(max_length = 50) is_pending = models.BooleanField() is_allowed = models.BooleanField() class Official_Site(Website): policy = models.ForeignKey(Policy_Detail) class Interviews(Website): policy = models.ForeignKey(Policy_Detail) class Frequently_Asked_Questions(Website): policy = models.ForeignKey(Policy_Detail) class Articles(Website): policy = models.ForeignKey(Policy_Detail) class Books(Website): policy = models.ForeignKey(Policy_Detail) class Petitions(Website): policy = models.ForeignKey(Policy_Detail) class Actions(Website): policy = models.ForeignKey(Policy_Detail) class Rallies(Website): policy = models.ForeignKey(Policy_Detail) class Events(models.Model): policy = models.ForeignKey(Policy_Detail) event_date = models.DateField(null=True) ... class Policy_User(models.Model): country = models.ForeignKey(Country) username = models.CharField(max_length = 30, default="Anonymous") class Vote(models.Model): policy = models.ForeignKey(Policy) p_user = models.ForeignKey(Policy_User) voting_date = models.DateField(auto_now_add = True) week_number = models.IntegerField(default = 1) vote_code = models.IntegerField(default = 1)

谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

这看起来更像是迁移与数据库状态不同步的问题。我不认为多对多的领域是主要问题,尽管它们可能通过使迁移更多来加剧它 复杂。

虽然您可以通过运行单个迁移,手动将数据库中的迁移标记为完整或其他繁琐的方法来解决此问题,但这是解决问题的最直接,最简洁的方法,并且因为您不喜欢t还有数据库中的任何数据。

  1. 从数据库中删除所有表。
  2. 删除所有Django应用程序目录中的migrations目录。
  3. 再次运行manage.py makemigrations然后manage.py migrate