Code

Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#2269 closed defect (fixed)

limit_choices_to should use to_field

Reported by: mir@… Owned by: adrian
Component: Core (Other) Version: master
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

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.

Attachments (0)

Change History (3)

comment:1 Changed 8 years ago by adrian

  • Status changed from new to assigned

comment:2 Changed 8 years ago by adrian

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 Changed 8 years ago by adrian

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

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.