Django - How Can I Make A Cell In A Table In The Admin *changelist* Interface Editable Only If It Is Null?
Solution 1:
If your field uses an input
element, such as a TextField
, add the readonly
attribute to the field's widget's attrs
dict in the changelist form's __init__
method. Something like this:
classPamphletChangeListForm(forms.ModelForm):
def__init__(self, *args, **kwargs):
super(PamphletChangeListForm, self).__init__(*args, **kwargs)
instance = kwargs.get('instance')
if instance:
self.fields['book'].queryset = Book.objects.filter(
size=instance.size
)
if instance.size isnotNone:
self.fields['size'].widget.attrs['readonly'] = 'readonly'
That won't protect you against a malicious user faking post data - for that you'd need to customize your admin further. But if you have malicious users on your admin you have bigger problems.
If your field uses a select
element, you have to change it more - select
attributes don't have readonly
as a supported attribute. Instead, you'll want a hidden input with the unchanging value, and a text representation so the user can see what the setting is. Django does not include such a widget, but you can define your own:
classLabeledHiddenInput(forms.widgets.HiddenInput):
defrender(self, name, value, attrs=None):
base_output = super(LabeledHiddenInput, self).render(name, value, attrs)
if value:
return base_output + unicode(value)
else:
return base_output
You might need more careful escaping or even some HTML formatting, this is just a quick example. Check the source code for the built in widgets if you need more examples.
Then you can use that widget instead of the default select:
if instance.size isnotNone:
self.fields['size'].widget = LabeledHiddenInput()
Post a Comment for "Django - How Can I Make A Cell In A Table In The Admin *changelist* Interface Editable Only If It Is Null?"