Opened 13 years ago
Closed 10 years ago
#16978 closed Bug (fixed)
Related models cannot have split() method
Reported by: | Mitar | Owned by: | Jonas Obrist |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | mmitar@… | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | yes |
Easy pickings: | no | UI/UX: | no |
Description
Related models cannot have split()
method because add_lazy_relation
(in db/models/fields/related.py
) assumes that something with split()
method must be string. I would propose that:
try: app_label, model_name = relation.split(".") except ValueError:
is changed to:
try: if isinstance(relation, ModelBase): raise ValueError app_label, model_name = relation.split(".") except ValueError:
Attachments (1)
Change History (11)
comment:1 by , 13 years ago
Component: | Documentation → Database layer (models, ORM) |
---|---|
Triage Stage: | Unreviewed → Accepted |
Version: | 1.3 → SVN |
comment:2 by , 13 years ago
Owner: | changed from | to
---|
comment:3 by , 13 years ago
I tried to reproduce the problem, but couldn't do so, what is needed to have this problem happen?
comment:4 by , 13 years ago
Owner: | changed from | to
---|
comment:5 by , 13 years ago
You have to check when add_lazy_relation
is called with class as first argument. I find two such cases:
- in
RelatedField
'scontribute_to_class
ifother._meta.pk is None
- if
order_with_respect_to
is configured
I do not remember how I triggered the first case. But the second case happens with, for example:
class RelatedModel(models.Model): foobar = models.IntegerField() def split(self): pass class SomeModel(models.Model): related_field = models.ForeignKey(RelatedModel) class Meta: order_with_respect_to = 'related_field'
comment:6 by , 13 years ago
Needs tests: | set |
---|
comment:7 by , 13 years ago
Has patch: | set |
---|---|
Needs tests: | unset |
Owner: | changed from | to
comment:8 by , 13 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
comment:9 by , 13 years ago
Patch needs improvement: | set |
---|---|
Triage Stage: | Ready for checkin → Accepted |
Apparently, the test passes without the fix in django/db/models/fields/related.py
.
comment:10 by , 10 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
It looks to me like the current code shouldn't have this problem.
Not sure the proposed patch is the best option, but this should certainly be fixed.