django m2m 模型不同步

Posted

技术标签:

【中文标题】django m2m 模型不同步【英文标题】:django m2m model not syncing 【发布时间】:2012-12-24 13:50:54 【问题描述】:

当我在新的 m2m 模型上运行 syncdb 时出现错误:

错误:一个或多个模型未验证: services.service: 'categories' 通过模型Service_Category指定m2m关系,尚未安装

我尝试使用上面列出的示例 https://docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-on-many-to-many-relationships

当我同步时它工作正常。所以我在下面的模型中没有得到正确的结果。不太确定为什么我的模型不起作用,以及它是否是一个简单的错字或其他原因。注意:模型使用简单的 m2m 关系运行良好,但它不喜欢这种方式。谢谢

from django.db import models

# Create your models here.




class Category(models.Model):

    name = models.CharField(max_length=50)
    slug = models.SlugField(max_length=50, unique=True,
                            help_text='Unique value for product page URL, created from name.')
    description = models.TextField()
    is_active = models.BooleanField(default=True)
    meta_keywords = models.CharField("Meta Keywords", max_length=255, blank = True, null = True,
                                     help_text='Content for description meta tag')
    meta_description = models.CharField(max_length = 255, blank = True, null = True,
                                        help_text = 'Content for description meta tag')
    created_at = models.DateTimeField(auto_now_add = True)
    updated_at = models.DateTimeField(auto_now = True)

    class Meta:
        #app_label = ''
        db_table = 'categories'
        ordering = ['created_at']
        verbose_name_plural = 'Categories'

    def __unicode__(self):
        return self.name

    @models.permalink   
    def get_absolute_url(self):
        return ('catalog_category', (), 'category_slug': self.slug)


class Service(models.Model):

    name = models.CharField(max_length=255, unique = True)
    slug = models.SlugField(max_length=255, unique = True,
                            help_text = 'Unique value for product page URL, created from name.')


    old_price = models.DecimalField(max_digits=9, decimal_places=2,
                                    blank = True, null = True, default = 0.0)
    image = models.CharField(max_length=50, blank = True)
    is_active = models.BooleanField(default = True)
    is_bestseller = models.BooleanField(default = False)
    is_featured = models.BooleanField(default = False)
    description = models.TextField()
    bullet1 = models.CharField(max_length=255, blank = True, null = True,
                               help_text = 'Option Bullet for description')
    bullet2 = models.CharField(max_length=255, blank = True, null = True,
                               help_text = 'Option Bullet for description')
    bullet3 = models.CharField(max_length=255, blank = True, null = True,
                               help_text = 'Option Bullet for description')
    bullet4 = models.CharField(max_length=255, blank = True, null = True,
                               help_text = 'Option Bullet for description')
    bullet5 = models.CharField(max_length=255, blank = True, null = True,
                               help_text = 'Option Bullet for description')    
    micro_current = models.BooleanField(default = False)

    meta_keywords = models.CharField(max_length = 255,blank = True, null = True, 
                                     help_text = 'Comma Delimited Set of SEO keywords for meta tag')
    meta_description = models.CharField(max_length = 255, blank = True, null = True,
                                        help_text = 'Content for description meta tag')
    created_at = models.DateTimeField(auto_now_add = True)
    updated_at = models.DateTimeField(auto_now = True)


    categories = models.ManyToManyField(Category, through='Service_Category')


    class Meta:
        #app_label = ''
        db_table = 'services'
        ordering = ['created_at']


    def __unicode__(self):
        return self.name


class Service_Category:

    category = models.ForeignKey(Category)
    service = models.ForeignKey(Service)
    micro_current = models.BooleanField(default = False)


    class Meta:
        #app_label = ''
        db_table = 'service_categories'

services.service: 'categories' 通过模型Service_Category指定m2m关系,尚未安装

【问题讨论】:

【参考方案1】:

您错过了正确定义中间模型

class Service_Category(models.Model):

    category = models.ForeignKey(Category)
    service = models.ForeignKey(Service)
    micro_current = models.BooleanField(default = False)


    class Meta:
        #app_label = ''
        db_table = 'service_categories'

我们必须继承我们从 models.Model 类中定义的每个模型。 休息一切似乎都很好。

【讨论】:

@mcddewitt:你可以接受这个答案,因为它可能对未来的人有所帮助。【参考方案2】:

Service_Category需要继承自models.Model

class Service_Category(models.Model):

【讨论】:

以上是关于django m2m 模型不同步的主要内容,如果未能解决你的问题,请参考以下文章

Django ManyToMany 通过不同步或迁移

模型在同步时不创建表

Django:通过模型实例化M2M

Django多对多(m2m)与同一模型的关系

如何根据 M2M 关系在 django 模型中自动填充 IntegerField?

Django - 如何构建适合的中间 m2m 模型? / 最佳实践