Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#2216 closed defect (fixed)

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

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

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 Changed 9 years ago by mir@…

  • 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 Changed 9 years ago by russellm

  • Owner changed from adrian to russellm

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

comment:3 Changed 9 years ago by russellm

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 Changed 9 years ago by mir@…

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 Changed 9 years ago by russellm

  • Resolution set to fixed
  • Status changed from new to closed

(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