Django makemigrations AttributeError: 'str' object has no attribute '_meta'

Django makemigrations AttributeError: 'str' object has no attribute '_meta'

我在玩 django 框架,我 运行 遇到了问题 运行 makemigration 命令。这是模型的副本及其生成的堆栈跟踪。 我已经看到几个帖子有同样的错误,但 none 使我在这里解决了我的问题。我正在使用 django 1.9.4

from django.db import models
import os, uuid

# Create your models here.
def video_directory_path(instance, folder):
    return os.path.join('video', str(instance.person.id), str(instance.video_id))

def photo_directory_path(instance, folder):
        return os.path.join('image', str(instance.person.id), str(instance.photo_id))


class Image(models.Model):
    name = models.CharField(max_length=128)
    photo_id = models.UUIDField(verbose_name='photo id', default=uuid.uuid4, editable=False, unique=True)
    photo = models.ImageField(upload_to=photo_directory_path)
    person = models.ForeignKey('Person', on_delete=models.CASCADE)
    movie = models.ForeignKey('Movie', on_delete=models.CASCADE)

    class Meta:
        db_table = 'Image'

    def __str__(self):
        return '[{0}- {1}]'.format(self.__class__.__name__, self.id)

    def __repr__(self):
        return self.__str__()


class Video(models.Model):
    name = models.CharField(max_length=128)
    video_id = models.UUIDField(verbose_name='video id', default=uuid.uuid4, editable=False, unique=True)
    video = models.FileField(upload_to=video_directory_path)
    person = models.ForeignKey('Person', on_delete=models.CASCADE)
    movie = models.ForeignKey('Movie', on_delete=models.CASCADE)

    class Meta:
        db_table = 'Video'

    def __str__(self):
        return '[{0}-{1}]'.format(self.__class__.__name__, self.id)

    def __repr__(self):
        return self.__str__()


class Filmography(models.Model):
    filmography = models.CharField(max_length=128, db_index=True)

    class Meta:
        db_table = 'Filmography'

    def __str__(self):
        return '[{0}-{1}]'.format(self.__class__.__name__, self.id)

    def __repr__(self):
        return self.__str__()


class Person(models.Model):
    bio = models.TextField()
    filmography = models.ManyToManyField('Filmography')
    photos = models.ManyToManyField(
        'Image',
        through='ImagePerson',
        through_fields=('person', 'photo')
    )

    class Meta:
        abstract = True

    def __str__(self):
        return '[{0}-{1}]'.format(self.__class__.__name__, self.id)

    def __repr__(self):
        return self.__str__()


class Profile(Person):
    first_name = models.CharField(max_length=128, verbose_name="first name")
    last_name = models.CharField(max_length=128, verbose_name="last name")
    dob = models.DateField()

    class Meta:
        index_together = ["first_name", "last_name"]
        db_table = 'Profile'


class Character(Person):
    name = models.CharField(max_length=128)

    class Meta:
        db_table = 'Character'


class Crew(models.Model):
    name = models.CharField(max_length=256)
    members = models.ManyToManyField(
        'Profile',
        through='MovieCrew',
        through_fields=('crew', 'profile'),
    )

    class Meta:
        db_table = 'Crew'

        def __str__(self):
            return '{0}: {1}'.format(self.__class__.__name__, self.name)

    def __repr__(self):
        return self.__str__()


class MovieCrew(models.Model):
    crew = models.ForeignKey(
        'Crew',
        on_delete=models.CASCADE,
    )
    profile = models.ForeignKey(
        'Profile',
        on_delete=models.CASCADE,
    )
    role = models.CharField(max_length=256)

    class Meta:
        db_table = 'MovieCrew'


class Genre(models.Model):
    genre = models.CharField(max_length=128, db_index=True)

    class Meta:
        db_table = 'Genre'


class Movie(models.Model):
    name = models.CharField(max_length=128, db_index=True)
    summary = models.CharField(max_length=256)
    story = models.CharField(max_length=256)
    release_date = models.DateField()

    crew = models.OneToOneField(
        'Crew',
        on_delete=models.CASCADE,
    )
    genre = models.ManyToManyField('Genre')
    photos = models.ManyToManyField('Image')
    videos = models.ManyToManyField('Video')

    class Meta:
        db_table = 'Movie'
        get_latest_by = 'release_date'

    def __str__(self):
        return '[{0}-{1}]'.format(self.__class__.__name__, self.name)

    def __repr__(self):
        return self.__str__()

和错误堆栈跟踪

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python34\lib\site-packages\django\core\management\base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Python34\lib\site-packages\django\core\management\base.py", line 398, in execute
    self.check()
  File "C:\Python34\lib\site-packages\django\core\management\base.py", line 426, in check
    include_deployment_checks=include_deployment_checks,
  File "C:\Python34\lib\site-packages\django\core\checks\registry.py", line 75, in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:\Python34\lib\site-packages\django\core\checks\model_checks.py", line 28, in check_all_models
    errors.extend(model.check(**kwargs))
  File "C:\Python34\lib\site-packages\django\db\models\base.py", line 1170, in check
    errors.extend(cls._check_fields(**kwargs))
  File "C:\Python34\lib\site-packages\django\db\models\base.py", line 1249, in _check_fields
    errors.extend(field.check(from_model=cls, **kwargs))
  File "C:\Python34\lib\site-packages\django\db\models\fields\related.py", line 1165, in check
    errors.extend(self._check_relationship_model(**kwargs))
  File "C:\Python34\lib\site-packages\django\db\models\fields\related.py", line 1366, in _check_relationship_model
    for f in through._meta.fields:
AttributeError: 'str' object has no attribute '_meta'

该文件中似乎没有名为 ImagePerson 的 class,您将 through(通过 table)设置为在 Person.

上的 M2M 中

您的 Crew 有缩进问题 class? 您的 str 方法似乎是在 Meta 上定义的:

class Crew(models.Model):
    name = models.CharField(max_length=256)
    members = models.ManyToManyField(
        'Profile',
        through='MovieCrew',
        through_fields=('crew', 'profile'),
    )

    class Meta:
        db_table = 'Crew'


        def __str__(self):
            return '{0}: {1}'.format(self.__class__.__name__, self.name)

    def __repr__(self):
        return self.__str__()

这真的是你想要的吗?

对于一些遇到此错误的人,不要忘记将新创建的应用程序添加到 Django 的 INSTALLED_APPS 中,以便识别 ManyToManythrough= 中介模型。

确保你做到了 python manage.py makemigrationspython manage.py migrate

如果您已经这样做了,请检查

serializers.py

class Meta:
    model = 'bla bla' 
    fields = '__all__'

我在字符串中写入模型名称时出现此错误 ('') 如果您也犯了同样的错误,请检查并 删除 ''