Code

Opened 5 months ago

Last modified 7 weeks ago

#21419 new Bug

Model validation inspects models not present in INSTALLED_APPS

Reported by: jcugat 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.

Attachments (0)

Change History (5)

comment:1 Changed 5 months ago by timo

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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

comment:2 Changed 5 months ago by loic84

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 Changed 5 months ago by aaugustin

  • Triage Stage changed from Unreviewed to Accepted

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 Changed 7 weeks ago by jcugat

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 Changed 7 weeks ago by aaugustin

(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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as new
The owner will be changed from nobody to anonymous. Next status will be 'assigned'
as The resolution will be set. Next status will be 'closed'
Author


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

 
Note: See TracTickets for help on using tickets.