Django

Code

Ticket #1839 (closed: fixed)

Opened 2 years ago

Last modified 1 year ago

ForeignKey('self') with attribute name like model name breaks AddManipulator().save()

Reported by: mir@noris.de Assigned to: adrian
Milestone: Component: Database wrapper
Version: SVN Keywords:
Cc: django@attardi.org, gabor@nekomancer.net Triage Stage: Accepted
Has patch: 0 Needs documentation: 0
Needs tests: 1 Patch needs improvement: 0

Description

I use this model:

class Another(models.Model):
    name = models.CharField(maxlength=20, core=True)
    another = models.ForeignKey('self', null=True, related_name='child_set')

Note that the field name another is like the model name, only lower case.

The following code fails:

Another.AddManipulator().save(dict(id='3', name='Another root category', another=None))

This might be an intended restriction, but then it should be checked by model validation. When it strikes, it's rather surprising because you don't see any connection to the attribute name in the backtrace. And it's probably a common thing to name the self relationship after the table.

This is the exception:

Exception:   File "/home/mir/src/django/trunk/tests/doctest.py", line 1243, in __run
    compileflags, 1) in test.globs
  File "<doctest m2o_recursive[10]>", line 1, in ?
    anotherone = Another.AddManipulator().save(dict(id='3', name='Another root category', another=None))
  File "/home/mir/src/django/active/django/db/models/manipulators.py", line 75, in __init__
    self.fields.extend(f.get_manipulator_fields(self.opts, self, self.change, fol))
  File "/home/mir/src/django/active/django/db/models/related.py", line 112, in get_manipulator_fields
    if follow.get(f.name, False):
AttributeError: 'bool' object has no attribute 'get'

db.models.related.RelatedObject?.get_manipulator_fields expects a dictionary in follow. But db.models.maniplators.init() sends True.

Attachments

models.py (1.4 kB) - added by mir@noris.de on 05/11/06 06:53:44.
unit test demonstrating this problem
m2o_recursive.diff (0.8 kB) - added by mir@noris.de on 05/11/06 06:54:51.
... or as a patch to existing m2o_recursive test, if you prefer

Change History

05/11/06 06:53:44 changed by mir@noris.de

  • attachment models.py added.

unit test demonstrating this problem

05/11/06 06:54:51 changed by mir@noris.de

  • attachment m2o_recursive.diff added.

... or as a patch to existing m2o_recursive test, if you prefer

05/13/06 12:50:43 changed by Stefano J. Attardi

Could have searched... #1826.

05/14/06 01:35:12 changed by mir@noris.de

Sorry. I do search, but sometime I overlook things.

Anyway, ticket #1826 demonstrates that *any* field that has the same name as the attribute is sufficient to trigger this bug when there's a ForeignKey?('self') *anywhere* in the model. It needn't be the ForeignKey? field itself.

05/15/06 17:37:13 changed by Stefano J. Attardi <django@attardi.org>

  • cc set to django@attardi.org.

06/23/06 04:05:47 changed by mir@noris.de

Note: This is very similar to #2216 (Filtering by a field having same name as the model results in AttributeError?)

06/27/06 20:49:05 changed by russellm

Doesn't appear to be the same problem as #2216. There is no clash in query terms/accessors - the problem seems to be in the construction of the follow term for related fields.

06/30/06 04:59:26 changed by anonymous

  • owner changed from adrian to russellm.

07/30/06 10:51:09 changed by anonymous

  • cc changed from django@attardi.org to django@attardi.org, gabor@nekomancer.net.

09/07/06 07:39:01 changed by russellm

  • owner changed from russellm to adrian.

This should be cleaned up as part of the manipulator refactoring effort.

11/20/06 22:13:59 changed by anonymous

still no fix for this? ouch! got the same problem here...

02/26/07 23:57:26 changed by Gary Wilson <gary.wilson@gmail.com>

  • stage changed from Unreviewed to Accepted.

02/26/07 23:58:14 changed by Gary Wilson <gary.wilson@gmail.com>

although manipulators will be going away when newforms is complete, so this might not be worth fixing.

02/27/07 01:14:13 changed by ubernostrum

newforms-admin will indeed fix this; the problem is with the follow dict for manipulators.

03/05/07 09:21:59 changed by Gary Wilson <gary.wilson@gmail.com>

  • needs_tests set to 1.

Even if this doesn't get fixed in manipulators, we should make certain it works with newforms with some tests.

03/07/07 11:03:44 changed by ubernostrum

(In [4673]) 0.91-bugfixes: Fixed #999 by resolving name clash in the metasystem which could confuse manipulators about which fields they should follow. Refs #1808, #1826, #1839 and #2415, which are variations of this that persist in trunk.

03/07/07 21:21:35 changed by mtredinnick

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

(In [4676]) Fixed #1839, #2415, #2536 -- Fixed a generated name clash that was common in self-referential and circular relations. A lot of community debugging went into this fix, so thanks to bmurdock@gmail.com, Marek Kubica, ramiro, Michael Radziej (the last two giving test cases showing the problem) and James Bennett (who did the hard work to actually diagnose the true problem and fix it).


Add/Change #1839 (ForeignKey('self') with attribute name like model name breaks AddManipulator().save())




Change Properties
Action