Opened 19 years ago

Closed 19 years ago

Last modified 18 years ago

#2216 closed defect (fixed)

Filtering by a field having same name as the model results in AttributeError

Reported by: anonymous Owned by: Russell Keith-Magee
Component: Database layer (models, ORM) Version: dev
Severity: normal Keywords:
Cc: mir@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

For model

class Color(models.Model):
  color = models.TextField(maxlength=100)
  similar_colors = models.ManyToManyField("self")

filtering does not work by field "color": Color.objects.filter(color="Light") results in AttributeError: 'RelatedObject' object has no attribute 'column'

Without the similar_colors relation, the filtering works fine.

Change History (5)

comment:1 by mir@…, 19 years ago

Cc: mir@… added

Looks very similar to #1839 (ForeignKey('self') with attribute name like model name breaks AddManipulator().save()).

The error message is slightly different, so it's only *perhaps* a duplicate ;-)

comment:2 by Russell Keith-Magee, 19 years ago

Owner: changed from Adrian Holovaty to Russell Keith-Magee

Looks like a problem with related_query_name() on RelatedObject returning the wrong name. I'll look into it.

comment:3 by Russell Keith-Magee, 19 years ago

It turns out that related_query_name() is ok - your model is an edge case invalid model that isn't currently identified. The field name color clashes with the query term for the m2m relation with self. We don't currently validate for clashes in query terms - but we should. I'm working on a fix.

comment:4 by mir@…, 19 years ago

Russell, can you have a quick look at #1839 if your comment applies to it, too? It's also about the field_name==model_name, but in a on-to-many relationship, and the error message is different.

Michael

comment:5 by Russell Keith-Magee, 19 years ago

Resolution: fixed
Status: newclosed

(In [3215]) Fixes #2216 -- Added extra tests for model validity, and cleaned up warning messages for existing tests. Models are now invalid if the query string required to access a field (or related object) would clash with the name of another field (or related object). Previous tests only checked the accessor names, not the query string.

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