#2269 closed defect (fixed)
limit_choices_to should use to_field
| Reported by: | Owned by: | Adrian Holovaty | |
|---|---|---|---|
| Component: | Core (Other) | Version: | dev |
| Severity: | normal | Keywords: | |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
When I do something like this:
class Blupp(models.Model):
kind = IntegerField()
another_key=IntegerField()
class Blipp(models.Model):
wont_work = ForeignKey(Blupp, limit_choices_to({'kind__exact': 1}), to_field='another_key')
Then the choices for Blipp will be computed to the primary key of Blupp, no to another_key
I blame Field.get_choices(), here's the code:
def get_choices(self, include_blank=True, blank_choice=BLANK_CHOICE_DASH):
"Returns a list of tuples used as SelectField choices for this field."
first_choice = include_blank and blank_choice or []
if self.choices:
return first_choice + list(self.choices)
rel_model = self.rel.to
return first_choice + [(x._get_pk_val(), str(x))
for x in rel_model._default_manager.complex_filter(self.rel.limit_choices_to)]
In the second last line, x._get_pk_val() needs to be replaced.
Change History (3)
comment:1 by , 19 years ago
| Status: | new → assigned |
|---|
comment:2 by , 19 years ago
comment:3 by , 19 years ago
| Resolution: | → fixed |
|---|---|
| Status: | assigned → closed |
Note:
See TracTickets
for help on using tickets.
Here are the example models rewritten with correct syntax:
class Blupp(models.Model): kind = models.IntegerField() another_key = models.IntegerField() class Blipp(models.Model): wont_work = models.ForeignKey(Blupp, limit_choices_to={'kind__exact': 1}, to_field='another_key')