Django - ValueError:ModelState.fields不能引用模型类 - " tags.to"确实。请改用字符串引用

时间:2016-01-17 15:12:42

标签: python django django-1.9

我有这个迁移:

# encoding: utf8
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

    dependencies = [
        ('main', '0013_post_tags'),
    ]

    operations = [
        migrations.AddField(
            model_name='guidauser',
            name='avatar',
            field=models.ImageField(null=True, upload_to=''),
            preserve_default=True,
        ),
    ]

它与Django 1.7.9完美配合。但现在我已将项目升级到Django 1.9。如果我尝试部署它(本地),那么' manage.py migrate'导致错误:

enter image description here

但是,它适用于Django 1.8.8

models.py:

class GuidaUser(User):
    GENDER_MALE = 'male'
    GENDER_FEMALE = 'female'

    GENDER_CHOICES = (
        (GENDER_MALE, 'Чоловіча'),
        (GENDER_FEMALE, 'Жіноча'),
    )
    about_yourself = models.TextField(max_length=150, blank=True)
    contacts = models.ForeignKey(GuidaUserContacts, blank=True, null=True, related_name='contacts')
    avatar = models.ImageField(upload_to='avatars', null=True, blank=True)
    gender = models.CharField(choices=GENDER_CHOICES, default=GENDER_MALE, blank=True, max_length=10)
    birthday = models.DateField(null=True, blank=True)
    #work = models.CharField(max_length=50)
    ip_register = models.GenericIPAddressField(blank=True, null=True, unique=True, default=None)
    has_username = models.BooleanField(default=True)

    objects = UserManager()

而且似乎这个错误与之前的迁移有关(但是Django应用它),所以这里是:

class Migration(migrations.Migration):

dependencies = [
    ('main', '0012_auto_20140721_1650'),
    ('taggit', '__first__'),
]

operations = [
    migrations.AddField(
        model_name='post',
        name='tags',
        field=taggit.managers.TaggableManager(to=taggit.models.Tag, through=taggit.models.TaggedItem, help_text='A comma-separated list of tags.', verbose_name='Tags'),
        preserve_default=True,
    ),
]

那么,如何修复呢?

1 个答案:

答案 0 :(得分:2)

替换

field=taggit.managers.TaggableManager(to=taggit.models.Tag,
                                      through=taggit.models.TaggedItem,
                                      help_text='A comma-separated list of tags.',
                                      verbose_name='Tags')

使用

field=taggit.managers.TaggableManager(to='taggit.models.Tag',
                                      through='taggit.models.TaggedItem',
                                      help_text='A comma-separated list of tags.',
                                      verbose_name='Tags')

错误告诉您不能在字段中包含类,而是使用类名作为字符串。