Opened 11 years ago

Closed 10 years ago

#21419 closed Bug (fixed)

Model validation inspects models not present in INSTALLED_APPS

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

Description

I have uploaded a minimal project showing this issue here: https://github.com/jcugat/django-model-validation-bug

This project has two apps (app_a and app_b). app_a has two models: Abstract_User_A (a subclass of Django's AbstractUser), and User_A (a concrete subclass of Abstract_User_A). app_b has only one model: User_B (also a concrete subclass of Abstract_User_A).

In settings.py, only app_b is in INSTALLED_APPS, and the User model is swapped: AUTH_USER_MODEL = 'app_b.User_B'.

Executing manage.py validate shows the following errors:

CommandError: One or more models did not validate:
app_b.user_b: Accessor for m2m field 'groups' clashes with related m2m field 'Group.user_set'. Add a related_name argument to the definition for 'groups'.
app_b.user_b: Accessor for m2m field 'user_permissions' clashes with related m2m field 'Permission.user_set'. Add a related_name argument to the definition for 'user_permissions'.

The problem is that somehow the validate command is inspecting the model User_A althought it's not installed.

This works OK in Django 1.5.5, but fails in Django 1.6 and master.

Change History (6)

comment:1 by Tim Graham, 11 years ago

Not sure what the answer is here. Using git bisect, I traced the change to f325f86971bf7fc30e3daafc9835f66cd7951e3a.

comment:2 by loic84, 11 years ago

This comment seems to be related: https://code.djangoproject.com/ticket/20244#comment:21 (#20244 is the ticket fixed by the commit that Tim bisected).

comment:3 by Aymeric Augustin, 11 years ago

Triage Stage: UnreviewedAccepted

I understand this is a local regression in 1.6, but I believe the underlying problem in more general. Whenever Django imports a model (abstract or not) from an application (app_a in this example), that application is added to the app cache.

As a consequence the app cache may end up containing applications that aren't in INSTALLED_APPS. I hate extending the scope of app-loading (#3591), but I believe that's our only opportunity to fix this.

What's the correct behavior when a user imports a model from an application that isn't in INSTALLED_APPS? Currently, the model will be added to the app cache and everything works. I suspect lots of project implicitly rely on this behavior. I think we should remove this possibility with a deprecation path. This reports asks for the opposite behavior, but that would result in confusing behavior in my opinion.

Regardless of this analysis, we could fix this particular regression if possible at all.

comment:4 by Josep Cugat, 11 years ago

It seems that the App-Loading refactor Aymeric did has fixed this bug. Bisecting the history it seems the fixing commit is [9f13c3328199d2fa70235cdc63bb06b1efc5b117]. Not sure if it's a side effect or intended behaviour, but is working now :)

comment:5 by Aymeric Augustin, 11 years ago

(In case that's not clear, I'm Aymeric.)

This is good news. I can see why the commit you linked to fixed this issue in 1.7.

It's still a regression in 1.6. We could try to fix this in the stable/1.6.x. Unfortunately, I'm not sure how, short of reverting the offending commit and re-creating #20244.

comment:6 by Aymeric Augustin, 10 years ago

Resolution: fixed
Status: newclosed

Since this is fixed in 1.7, I'm going to close the ticket. Please reopen if you have a proposal to improve the situation in 1.6.

Note: See TracTickets for help on using tickets.
Back to Top