0

ModelAdmin

class InstrumentAdmin(admin.ModelAdmin):    
    def get_form(self,request, obj=None, **kwargs):
        if obj:
            return UpdateForm
        else:
            return CreateForm

Update Form

class UpdateForm(forms.ModelForm):
    connector = forms.ModelChoiceField(queryset=Connector.objects.all(), widget=forms.Select(attrs={'disabled':'disabled'}), initial='multiplate')

    class Meta:
        model = Instrument
        fields = ['connector','name']

I have set initial value for the ChoiceField. But when i access the change of the model and Save, it prompt me "This field is required" for the field connector.

How can I make the field not mandatory in this case? Because it will always have a value there.

soohan
  • 137
  • 1
  • 9

2 Answers2

0

Fixes:

  1. initial=pk instead of name
  2. Add required=False
soohan
  • 137
  • 1
  • 9
0

The problem is with your attributes. Change:

attrs={'disabled':'disabled'}

to

attrs={'readonly':'readonly'}

Disabled does not send back data to the server while readonly does it.

  • Using attrs={'readonly':'readonly'} help in showing data. But the dropdown still able to change using readonly – soohan Sep 26 '16 at 06:35
  • The selected answer of this would answer that: http://stackoverflow.com/questions/8100351/how-to-make-a-dropdown-readonly-using-jquery – Ruqaiya Saad Sep 26 '16 at 07:22