15๐
โ
Iโd recommend specifying a form to use for the model, and in that form you can set whatever attributes you want to read only.
#forms.py
class AuthorForm(forms.ModelForm):
class Meta:
model = Author
def __init__(self, *args, **kwargs):
super(AuthorForm, self).__init__(*args, **kwargs)
if self.instance.id:
self.fields['weight'].widget.attrs['readonly'] = True
#views.py
AuthorFormSet = modelformset_factory(Author, extra=2, form=AuthorForm)
๐คj_syk
1๐
You can also put in your template :
{{form.management_form}}
{% for i in form %}
<p>{{ i.instance.readonly_field }}</p>
{{i.as_p}}
{% endfor %}
and not put the readonly_field in ModelForm.Meta.fields.
๐คmy_pseudo
- Display user group in admin interface?
- How to pass the remote IP to a proxied service? โ Nginx
- Pycharm error: Improperly configured
- How to call asynchronous function in Django?
- Django โ How ModelChoiceField queryset's works?
0๐
just need to check if the instance has id, like this:
if self.instance.id
before setting it as read-only
๐คCauane Andrade
- Django: Display a custom error message for admin validation error
- Meta.fields contains a field that isn't defined on this FilterSet: ****
- Django. Invalid keyword argument for this function. ManyToMany
- Celery: launch task on start
- Htaccess on heroku for django app
-2๐
I used python long back. Hope this helps . But if you wish to control fields display using jquery
$('.class').attr('readonly', true);
or
$('#id').attr('readonly', true);
๐คstackex
Source:stackexchange.com