Ticket #8648: to_field.diff

File to_field.diff, 1.5 KB (added by Karen Tracey, 16 years ago)
  • django/contrib/admin/widgets.py

     
    114114            (related_url, url, name))
    115115        output.append('<img src="%simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>' % settings.ADMIN_MEDIA_PREFIX)
    116116        if value:
     117            # if to_field for this relation is not the primary key field, transform value to
     118            # target's pk value so selected choice is correct in the widget display
     119            if hasattr(self.rel, 'field_name') and self.rel.field_name != self.rel.to._meta.pk.attname:
     120                value = self.rel.to._default_manager.get(**{self.rel.field_name: value}).pk
    117121            output.append(self.label_for_value(value))
    118122        return mark_safe(u''.join(output))
    119123   
     
    184188
    185189    def render(self, name, value, *args, **kwargs):
    186190        rel_to = self.rel.to
     191        # if to_field for this relation is not the primary key field, transform value to
     192        # target's pk value so selected choice is correct in the widget display
     193        if value and self.rel.field_name != rel_to._meta.pk.attname:
     194            value = rel_to._default_manager.get(**{self.rel.field_name: value}).pk
    187195        related_url = '../../../%s/%s/' % (rel_to._meta.app_label, rel_to._meta.object_name.lower())
    188196        self.widget.choices = self.choices
    189197        output = [self.widget.render(name, value, *args, **kwargs)]
Back to Top