Code

Opened 5 months ago

Closed 4 months ago

#21411 closed Bug (worksforme)

CommandError: One or more models did not validate but backtrace not saying anything useful

Reported by: artscoop Owned by: nobody
Component: Documentation Version: 1.6
Severity: Normal Keywords: validate
Cc: bmispelon Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hi,
There is a typo in my code (30 000 lines), which I cannot find. I cannot runserver, validate or shell_plus (django_extensions). I've had this problem with many versions of Django, but somehow managed to find where the error was. This time, I can't, and all I get is this cryptic error, absolutely not related to the position of the error, even with the --traceback and verbosity options.

Traceback (most recent call last):
  File "/home/username/virtualenv/project/one/manage.py", line 8, in <module>
    execute_from_command_line(sys.argv)
  File "/home/username/virtualenv/project/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "/home/username/virtualenv/project/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/username/virtualenv/project/local/lib/python2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/username/virtualenv/project/local/lib/python2.7/site-packages/django/core/management/base.py", line 285, in execute
    output = self.handle(*args, **options)
  File "/home/username/virtualenv/project/local/lib/python2.7/site-packages/django/core/management/base.py", line 415, in handle
    return self.handle_noargs(**options)
  File "/home/username/virtualenv/project/local/lib/python2.7/site-packages/django/core/management/commands/validate.py", line 10, in handle_noargs
    self.validate(display_num_errors=True)
  File "/home/username/virtualenv/project/local/lib/python2.7/site-packages/django/core/management/base.py", line 314, in validate
    raise CommandError("One or more models did not validate:\n%s" % error_text)
django.core.management.base.CommandError: One or more models did not validate:
menu.bookmark: 'user' has a relation with model user.User, which has either not been installed or is abstract.
reversion.revision: 'user' has a relation with model user.User, which has either not been installed or is abstract.
auth.user: Model has been swapped out for 'user.User' which has not been installed or is abstract.
moderation.moderatedobject: 'moderated_by' has a relation with model user.User, which has either not been installed or is abstract.
moderation.moderatedobject: 'changed_by' has a relation with model user.User, which has either not been installed or is abstract.
admin.logentry: 'user' has a relation with model user.User, which has either not been installed or is abstract.
dashboard.dashboardpreferences: 'user' has a relation with model user.User, which has either not been installed or is abstract.

In the shell command, I can import the User model and several others, so I know there is no typo in this model.

Attachments (0)

Change History (8)

comment:1 Changed 5 months ago by bmispelon

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

Hi,

I'm not sure how the error message can be improved. Do you have suggestions?

Your problem seems to be that you have AUTH_USER_MODEL = 'user.User' in your settings but the user app is not in your INSTALLED_APPS.
I've been able to gather this from the error messages so they can't be as useless as you make them to be.

There's always room for improvement though so if you have ideas on how to make these particular error messages more helpful, please share.
In the meantime, I'll close this ticket as invalid.
Please reopen it if you have a concrete proposal or if you want to point out what you think is not clear in the error messages.

If you're still stuck, you can reach out to our support channels [1] for help.

Thanks.
[1] https://docs.djangoproject.com/en/1.6/faq/help/#how-do-i-do-x-why-doesn-t-y-work-where-can-i-go-to-get-help

comment:2 Changed 5 months ago by artscoop

Well, the user app is obviously in INSTALLED_APPS. This exact error occurred to me before, just because of an import error in a file in another app (which is something completely unrelated). Everything worked yesterday but some edit somewhere broke everything, I cannot find where, the compiler in Eclipse shows no import error in any file.
I am absolutely positive that the error message provided by Django is totally useless here : The validation only reports errors in models fields and so on. In our situation, user.User cannot be imported because of another error, which is not raised.

comment:3 Changed 5 months ago by artscoop

  • Resolution invalid deleted
  • Status changed from closed to new

Sorry, but this really is a bug in Django (found a similar ticket where the bug was introduced in 1.1)
Please see my comment above.

comment:4 Changed 5 months ago by artscoop

Found. I don't know how it worked before now and how it suddenly stopped working, but I found the culprit. One third-party app models file defined User = get_user_model(), and used this as a ForeignKey target, instead of using a label. I suppose that at this time, the user model was not in the AppCache, which raised the error. What is bugging me is that this exact code had been working flawlessly for months.

I really think the Django documentation should warn against using classes directly as ForeignKey targets, and recommend using the app.model string representation, especially when linking to the user model. This behaviour prevents all circular and "too early" imports.
Even better, Django itself should, at some time, deprecate using plain model classes as ForeignKey targets.

Last edited 5 months ago by artscoop (previous) (diff)

comment:5 Changed 5 months ago by bmispelon

  • Cc bmispelon added
  • Type changed from Uncategorized to Bug

Hi,

Thanks for reopening the ticket.
It seems that your configuration is triggering a bug in the app-loading mechanism of django.

Could you provide some steps on how to reproduce this issue (as it is now, I'm not able to reproduce the behavior you describe)?
Alternatively, since you say that things were working at some point, are you able to pinpoint the failure to a particular Django commit?

comment:6 follow-up: Changed 5 months ago by aaugustin

  • Component changed from Core (Management commands) to Documentation
  • Triage Stage changed from Unreviewed to Accepted

We've seen similar issues and reached the conclusion that the proper way to link to the user model is:

    user = models.ForeignKey(settings.AUTH_USER_MODEL)

We should document this.

comment:7 Changed 5 months ago by artscoop

I forgot about this ticket.
@bmispelon, to reproduce this bug (which is simple actually), just add the django-activity-stream app (available on PyPI). If the package has not been fixed for this error, you should get the same error I've had.
@aaugustin, totally agree with it, when I refactored a project for Django 1.5, I had to use this to make the project work again.

comment:8 in reply to: ↑ 6 Changed 4 months ago by ramiro

  • Resolution set to worksforme
  • Status changed from new to closed

Replying to aaugustin:

We've seen similar issues and reached the conclusion that the proper way to link to the user model is:

    user = models.ForeignKey(settings.AUTH_USER_MODEL)

We should document this.

It is documented already: https://docs.djangoproject.com/en/1.5/topics/auth/customizing/#django.contrib.auth.get_user_model

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.