Opened 18 years ago

Closed 18 years ago

Last modified 18 years ago

#2269 closed defect (fixed)

limit_choices_to should use to_field

Reported by: mir@… 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 Adrian Holovaty, 18 years ago

Status: newassigned

comment:2 by Adrian Holovaty, 18 years ago

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')

comment:3 by Adrian Holovaty, 18 years ago

Resolution: fixed
Status: assignedclosed

(In [3507]) Fixed #2269 -- limit_choices_to now works properly with a custom ForeignKey.to_field. Thanks for reporting, mir@…

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