Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

python - Django Makemigrations not working with ForeignKey field to an app that has AppConfig

We have an app called sessions, and since Django has similar django.contrib.sessions app, we need to create an AppConfig to fix the "duplicate app" error, that is, we set the app label as common.sessions.

So, we have this folder/files structure:

common
    sessions
        __init__.py
        apps.py
        models.py

__init__.py

default_app_config = 'common.sessions.apps.CommonSessionsConfig'

apps.py

class CommonSessionsConfig(AppConfig):
    name = 'common.sessions'
    label = 'common.sessions'

models.py

class SessionEventType(models.Model):
    name = models.CharField()

class SessionEventTypeMapping(models.Model):
    session_event_type = models.ForeignKey(SessionEventType)

settings.py

INSTALLED_APPS = (
    'common.sessions',
    ...
)

Running manage.py migrations will throw this error:

  File "/virtualenv/backoffice/lib/python2.7/site-packages/django/core/management/commands/makemigrations.py", line 125, in handle
    migration_name=self.migration_name,
  File "/virtualenv/backoffice/lib/python2.7/site-packages/django/db/migrations/autodetector.py", line 43, in changes
    changes = self._detect_changes(convert_apps, graph)
  File "/virtualenv/backoffice/lib/python2.7/site-packages/django/db/migrations/autodetector.py", line 111, in _detect_changes
    self.new_apps = self.to_state.apps
  File "/virtualenv/backoffice/lib/python2.7/site-packages/django/utils/functional.py", line 59, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/virtualenv/backoffice/lib/python2.7/site-packages/django/db/migrations/state.py", line 166, in apps
    return StateApps(self.real_apps, self.models)
  File "/virtualenv/backoffice/lib/python2.7/site-packages/django/db/migrations/state.py", line 248, in __init__
    raise ValueError(msg.format(field=operations[0][1], model=lookup_model))
ValueError: Lookup failed for model referenced by field common.sessions.SessionEventTypeMapping.session_event_type: common.sessions.common.sessions.SessionEventType

Apparently, it fails on this part and could not locate the SessionEventType class/model even though they're just in the same models.py file, and ended up looking for common.sessions.common.sessions.SessionEventType which is strange:

session_event_type = models.ForeignKey(SessionEventType)

Tried also this variant:

session_event_type = models.ForeignKey('common.sessions.SessionEventType')

but it didn't help, as well as this related issue. By the way, we're still using Py2/Django1.8 since it's a huge project and we couldn't upgrade the version easily. Are we missing something? Checked the AppConfig/ForeignKey docs but seems no related info for it. No thrown error also if we don't use an FK field (i.e. non-relation field migrations are ok). We just want the makemigrations to work.

question from:https://stackoverflow.com/questions/65625946/django-makemigrations-not-working-with-foreignkey-field-to-an-app-that-has-appco

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...