Django

Code

Ticket #2232 (closed: fixed)

Opened 2 years ago

Last modified 2 years ago

ManyRelatedManager is broken in certain conditions (can't get .all() from ManyToManyField)

Reported by: Maniac@SoftwareManiacs.Org Assigned to: mtredinnick
Milestone: Component: Database layer (models, ORM)
Version: Keywords:
Cc: Maniac@SoftwareManiacs.Org, mtredinnick Triage Stage: Unreviewed
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

Looks like changeset 3195 has broken ManyToManyRelations. This simple code shows the problem:

class Article(models.Model):
  authors = models.ManyToManyField(Author)

...

article = Article.objects.all()[0]
article.authors.all() # Exception

The exception is

TypeError?: Cannot resolve keyword 'article' into field

Attachments

recursive-import-fix.diff (1.3 kB) - added by mtredinnick on 06/26/06 10:37:40.
Partial fix

Change History

06/25/06 05:04:37 changed by anonymous

  • cc set to Maniac@SoftwareManiacs.Org.

06/25/06 05:39:05 changed by Maniac@SoftwareManiacs.Org

  • summary changed from ManyRelatedManager is broken (can't get .all() from ManyToManyField) to ManyRelatedManager is broken for cross-app relations (can't get .all() from ManyToManyField).

Testing it a bit further shows that this fails only in this two cases:

  1. if a models references another model that is in another app
  2. (this one is strange) In my app I have a User model that is OneToOne?-related to auth.contrib.models.User. My User has "roles = models.ManyToMany?(Role)" where Role is also in my app. _This_ "roles" relation stopped working as of changeset 3195...

06/25/06 09:49:34 changed by mtredinnick

(In [3202]) Fixed #1796 -- only load a single copy of each model, even when it is referenced via different import paths. Solves a problem with many-to-many relations being set up incorrectly. Thanks to Curtis Thompson, Luke Plant and Simon Willison for some excellent debugging of the problem. Refs #2232 (may also have fixed that).

06/25/06 10:23:27 changed by Maniac@SoftwareManiacs.Org

  • status changed from new to closed.
  • resolution set to fixed.

Indeed that fixes it.

06/26/06 05:14:12 changed by Maniac@SoftwareManiacs.Org

  • cc changed from Maniac@SoftwareManiacs.Org to Maniac@SoftwareManiacs.Org, mtredinnick.
  • status changed from closed to reopened.
  • resolution deleted.
  • summary changed from ManyRelatedManager is broken for cross-app relations (can't get .all() from ManyToManyField) to ManyRelatedManager is broken in certain conditions (can't get .all() from ManyToManyField).

Unfortunately this bug still appears even after 'abspath' fix in #1796. This makes me think that this is a separate issue. To summarize:

There are two models (simplified):

from django.contrib.auth.models import User as DjangoUser

class Role(models.Model):
  slug = models.SlugField(unique=True)

class User(models.Model):
  django_user = models.OneToOneField(DjangoUser)
  roles = models.ManyToManyField(Role, blank=True)

When running under dev server from the project's directory as current one by executing "./manage.py runserver" everything works fine.

When running under Apache with mod_python with these settings:

  <Location "/">
    SetHandler python-program
    PythonPath sys.path+['/usr/share/django','/home/maniac/project_parent']
    PythonHandler django.core.handlers.modpython
    SetEnv DJANGO_SETTINGS_MODULE project_name.settings
    PythonDebug On
  </Location>

... then accessing user.roles.all() results in "Cannot resolve keyword 'user' into field"

Apache is running as "www-data" user, dev server as my local user.

I'm absolutely at loss where to start thinkning about it...

06/26/06 05:37:04 changed by mtredinnick

  • owner changed from adrian to mtredinnick.
  • status changed from reopened to new.

06/26/06 10:37:40 changed by mtredinnick

  • attachment recursive-import-fix.diff added.

Partial fix

06/26/06 10:39:32 changed by mtredinnick

The partial fix I just attached fixes part of the problem. I found another way we were creating two instances of a model. Can you try it out and see if it helps in your setup?

06/26/06 11:09:29 changed by Maniac@SoftwareManiacs.Org

Yes, looks fixed with the patch. Checked both Apache and dev server this time.

P.S. I look forward to reading your promised explanation in the wiki. This whole issue seems to me a bit fragile, may be there should be some more robust way of registering apps...

06/26/06 11:20:58 changed by mtredinnick

(In [3212]) Fixed another path where imports were creating two instances of a model's class. Refs #1796, #2232.

06/27/06 20:55:05 changed by mtredinnick

  • status changed from new to closed.
  • resolution set to fixed.

As far as I can see, [3212] should have fixed the last problem in this area. Closing now.


Add/Change #2232 (ManyRelatedManager is broken in certain conditions (can't get .all() from ManyToManyField))




Change Properties
Action