Issue
I have some models including custom user like that:
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'
That worked just fine with the other models I already have until i'm trying to add just one more class:
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'), ]
Unfortunately i get en error trying to migrate:
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'
I am puzzled of this because i didn't change neither settings.py nor anything else. I can just remove reference to user in "Vote" class and it becomes ok again but unfortunately i need to keep it here. Most of the other models in the same file do have same reference to user class via ForeignKey and "get_user_model()" function (perhaps i tried to switch it with user class itself - same result)- and it works with only difference i added them few migrations ago.
django version 2.0.2 from 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'
Maybe anybody faced it ever or just knows the solution of how to get rid of this error?
Solution
The actual reason was:
class Meta(AbstractUser.Meta):
swappable = 'stack.User'
Proper using of this is placed here https://code.djangoproject.com/wiki/ContribAuthImprovements#Solution%202c:%20Generic%20swappable%20models and my variant is not correct at all as it showed in the docs.But more important that i even don't need these lines at all. I couldn't even pay attention on this initially because this code already was for pretty long time and somehow it worked because insertion of Meta class doesn't require changing of database per se. So 'makemigrations' cmds just considered that nothing was changed unless actual addition was done
Answered By - Artem Starovoitov
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.