Hello,
I'm having a bit of a problem providing a custom readonly field, which is
defined in the form.
The field is defined in the form, as such:
class Form(forms.modelForm):
field = myCustomField()
The field is also associated with a custom widget, which is set in the
form's __init__.
it is included in a fieldset with its name - 'field', and under normal
circumstances is writable. This works just fine.
However, if I add this field to the self.readonly_fields list in the
modelAdmin's change_view, django complains that it cannot find it anymore.
After a lot of debugging, I worked around that by defining a method in the
modelAdmin:
def fields(self, instance):
form = self.form(instance=instance)
widget = form.fields['field'].widget
widget.is_readonly = True
value = instance.some_field
return widget.render('field', value)
field.short_description = _('Lookup')
field.allow_tags = True
While this works, I don't feel it is the correct approach. I didn't even
find any documentation on it, and I have to instantiate the form again, just
to get the field's widget, plus I also have to construct the value again.
Is there a better way to achieve this? How can I get that the field is
read-only from the widget itself?
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.