我有一些模型,包括自定义用户喜欢:
class User(AbstractUser):
image = models.ImageField(upload_to=get_image_path, blank=True, null=True)
objects = NewUserManager()
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email']
def __str__(self):
return self.email
class Meta(AbstractUser.Meta):
swappable = 'stack.User'
这与我已经拥有的其他模型配合得很好,直到我试图再添加一个类:
class Vote(models.Model):
rate_type = models.BooleanField()
question = models.ForeignKey("Question", related_name='question_rate', on_delete=models.PROTECT)
answer = models.ForeignKey("Answer", related_name='answer_rate', on_delete=models.PROTECT)
user = models.ForeignKey(get_user_model(), on_delete=models.PROTECT)
class Meta:
unique_together = [('question', 'user'), ('answer', 'user'), ]
很遗憾,尝试迁移时出现错误:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/home/artem/.local/lib/python3.5/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/home/artem/.local/lib/python3.5/site-packages/django/core/management/__init__.py", line 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/artem/.local/lib/python3.5/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/artem/.local/lib/python3.5/site-packages/django/core/management/base.py", line 335, in execute
output = self.handle(*args, **options)
File "/home/artem/.local/lib/python3.5/site-packages/django/core/management/commands/makemigrations.py", line 159, in handle
migration_name=self.migration_name,
File "/home/artem/.local/lib/python3.5/site-packages/django/db/migrations/autodetector.py", line 44, in changes
changes = self._detect_changes(convert_apps, graph)
File "/home/artem/.local/lib/python3.5/site-packages/django/db/migrations/autodetector.py", line 192, in _detect_changes
self._build_migration_list(graph)
File "/home/artem/.local/lib/python3.5/site-packages/django/db/migrations/autodetector.py", line 270, in _build_migration_list
resolved_app_label, resolved_object_name = getattr(settings, dep[1]).split('.')
File "/home/artem/.local/lib/python3.5/site-packages/django/conf/__init__.py", line 57, in __getattr__
val = getattr(self._wrapped, name)
AttributeError: 'Settings' object has no attribute 'stack.User'
我对此感到困惑,因为我既没有更改settings.py,也没有更改其他任何内容。我可以删除“Vote”类中对用户的引用,然后它再次恢复正常,但不幸的是,我需要将其保留在这里。同一文件中的大多数其他模型确实通过ForeignKey和“get_user_model”对用户类有相同的引用()”函数(也许我试着用用户类本身切换它-相同的结果)-它的工作原理只有不同,我在几次迁移前添加了它们。
django版本2.0.2来自settings.py:
# Application definition
INSTALLED_APPS = [
'stack.apps.StackConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
AUTH_USER_MODEL = 'stack.User'
wsgi.py:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "StackOverflow.settings")
application = get_wsgi_application()
apps.py:
class StackConfig(AppConfig):
name = 'stack'
也许有人曾经面对过它,或者只是知道如何摆脱这个错误的解决方案?
3条答案
按热度按时间yacmzcpb1#
stack
应用程序应添加到您的INSTALLED_APPS
设置中:此外,要在外键中指定型号,可以使用
settings.AUTH_USER_MODEL
:xwmevbvl2#
实际原因是:
正确使用这个放在这里https://code.djangoproject.com/wiki/ContribAuthImprovements#Solution%202c:%20Generic%20swappable%20models和我的变种是不正确的,因为它在文档中显示的。但更重要的是,我甚至不需要这些行在所有。我甚至不能注意到这一点,因为这段代码已经相当长的时间,不知何故,它的工作,因为插入 meta类不需要改变数据库本身。所以'makemigrations'cmds只是认为没有任何改变,除非实际添加完成
t5zmwmid3#
另一个可能的解决方案
在我的情况下,
migrations/__init__.py
从我的应用程序(在OP的情况下stack
)文件夹中丢失。因此,作为一个解决方案,我遵循以下步骤:
1.在任何缺少
migrations/__init__.py
的应用程序/文件夹中创建migrations/__init__.py
(例如stack
)。1.清除迁移和pycache文件:
find . -path "*/migrations/*.py" -not -name "__init__.py" -delete && find . -path "*/migrations/*.pyc" -delete
1.迁移:
python manage.py makemigrations
1.迁移:
python manage.py migrate
这些步骤对我来说已经足够了。