Opened 8 years ago

Closed 8 years ago

#26213 closed Bug (duplicate)

Admin list_editable and ForeignKeys: Applying widget attributes with specific forms lead to strange off-by-one effects

Reported by: Amir Hadi Owned by: nobody
Component: contrib.admin Version: 1.9
Severity: Normal Keywords: admin list_editable widget ForeignKey
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

We want to make some foreign key fields in the Admin list_editable. Also we want to add some widget attributes to the field, i.e. the instance PK for each row, a disabled tag and so on.

When we use the following code snippet, we see that the first row does not get the widget attributes applied. Strangely the second row contains the instance PK of the first row, the third row contains the PK of the second and so on. The effects on the interface can be seen here http://s22.postimg.org/lk8kxa4ch/Screen_Shot_2016_02_12_at_10_25_25.png

class LeadAdminForm(forms.ModelForm):
    class Meta:
        model = Lead
        fields = ['user_reference', 'state']

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['user_reference'].widget.attrs['data-lead-id'] = self.instance.pk
        self.fields['state'].widget.attrs['data-lead-id'] = self.instance.pk
        self.fields['user_reference'].widget.attrs['disabled'] = 'disabled'
        self.fields['state'].widget.attrs['disabled'] = 'disabled'


class LeadAdmin(admin.ModelAdmin):
    list_display = ['id', 'user_reference', 'state']
    list_editable = ['user_reference', 'state']
    list_per_page = 3

    def get_changelist_form(self, request, **kwargs):
        return LeadAdminForm

    def changelist_view(self, request, extra_context=None):
        extra_context = extra_context or {}
        #extra_context['params'] = get_connection_parameters(request.user)
        return super().changelist_view(request, extra_context)

We tested this with Django 1.8.9 and Django 1.9.2. I have also attached a demo project. Unfortunately we could not find the issue in the code.

Attachments (1)

bug.zip (7.5 KB ) - added by Amir Hadi 8 years ago.
Demo Project to show the issue

Download all attachments as: .zip

Change History (4)

by Amir Hadi, 8 years ago

Attachment: bug.zip added

Demo Project to show the issue

comment:1 by Tim Graham, 8 years ago

Thanks for the sample project. It looks to me like you need to target the inner widget of RelatedFieldWidgetWrapper, e.g. self.fields['user_reference'].widget.widget.attrs['disabled'] = 'disabled'. The fact that the original line works (except in the first row) appears to be a manifestation of #12134. With the patch from that ticket, the original line won't work at all.

in reply to:  1 comment:2 by Amir Hadi, 8 years ago

Replying to timgraham:

Thanks for the sample project. It looks to me like you need to target the inner widget of RelatedFieldWidgetWrapper, e.g. self.fields['user_reference'].widget.widget.attrs['disabled'] = 'disabled'. The fact that the original line works (except in the first row) appears to be a manifestation of #12134. With the patch from that ticket, the original line won't work at all.

Okay that seems to work. Thank you very much.

comment:3 by Tim Graham, 8 years ago

Resolution: duplicate
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top