0

In my ModelForm I need to disable ForeignKey field. I tried this thing but the select is still enabled and as I can see in html code attribute wasn't added to widget. Here's my code ModelForm code:

class ZayvkiAdminForm(ModelForm):
    class Meta:
        model = Zayvki
    def __init__(self, *args, **kwargs):
        if not kwargs.get('instance', None):
            if not kwargs.get('initial', None):
                kwargs['initial'] = {}
            if not kwargs['initial'].get('nomer_zayvki', None):
                kwargs['initial']['nomer_zayvki'] = get_request_number()
        super(ZayvkiAdminForm, self).__init__(*args, **kwargs)
        instance = getattr(self, 'instance', None)
        if instance and instance.id:
            self.fields['tipe_zayvki'].required = False
            self.fields['tipe_zayvki'].widget.attrs['disabled'] = 'disabled'
            self.fields['nomer_zayvki'].widget.attrs['readonly'] = True

UPDATE: I can't use exclude or readonly attrs of ModelAdmin because I need user to be able to add something when he creates the object. But when the object is created, I wan't user just to see the value and not to edit it.

Community
  • 1
  • 1
Dmitrii Mikhailov
  • 5,053
  • 7
  • 43
  • 69
  • Can you post the whole code for your ModelForm? – knbk Jul 27 '13 at 19:22
  • Hm, I don't see anything wrong with your code. Are you using `./manage.py runserver` to run your server? If so, try adding the line `print instance` just after `instance = getattr(self, 'instance', None)`. When you open the edit page, you should see some output in the console running the server, somewhere between the other debug statements. Can you tell me what you see? – knbk Jul 28 '13 at 13:16
  • In instance variable there is my model object of course. When i'm debugging script I can see attribute 'disabled' attached to widget so I don't know why it doesn't work. – Dmitrii Mikhailov Jul 28 '13 at 14:16
  • Can you post the code in your admins.py? I've got no problems adding a dynamically disabled field in my own app. Also, what Django version are you using? – knbk Jul 28 '13 at 14:34
  • 1.5.1. I can disable text input too but it doesn't work for select. You mean post code from ModelAdmin class? – Dmitrii Mikhailov Jul 28 '13 at 15:50

2 Answers2

0

A MoedlAdmin can be the solution:

class CustomAdmin(admin.ModelAdmin):
    readonly_fields = ('tipe_zayvki',)

also there is a method named get_readonly_fields here an example:

class CustomAdmin(admin.ModelAdmin):
    def get_readonly_fields(self, request, obj=None):
        if obj:
            return ['tipe_zayvki']
        else:
            return []
Victor Castillo Torres
  • 10,581
  • 7
  • 40
  • 50
0

Try specifying exclude as mentioned in the Django docs. This will remove the field from being rendered in the form, which is a cleaner UX than a disabled form input field.

So for your example:

class MyModelForm(ModelForm):
    class Meta:
        exclude = ('tipe_zayvki', )
dhui
  • 492
  • 4
  • 7