﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
26213	Admin list_editable and ForeignKeys: Applying widget attributes with specific forms lead to strange off-by-one effects	Amir Hadi	nobody	"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."	Bug	closed	contrib.admin	1.9	Normal	duplicate	admin list_editable widget ForeignKey		Unreviewed	0	0	0	0	0	0
