I'm fairly new to django and am just starting to play with it as a
framework for web-based applications. The kinds of applications I
develop typically have some fields for entities that are non-editable,
with some of these being non-editable only some of the time (depending
on things like entity state, security level of user viewing the
entity), but with those fields being shown still in forms - just not
showing a form control, but instead the field value. I also follow a
pattern of when a user opens an entity to look at it in "read mode"
they're shown basically the same layout of fields as in "edit
mode" (with field labels) but the values of the fields are shown
instead of form controls.

There's not really anything in django that allows for this sort of
behaviour out of the box from what I can see... There's a notion of
"editable" for the model field, but that causes the field to excluded
(by fields_for_model) from a form entirely, and there's no notion of
display-value-only behaviour for widgets.

To me something along the following lines would be useful:
  * form fields have an editable attribute that defaults to true
    * when building a form from a model when a model field has the
attribute set to false then it still includes it as a field but sets
the editable attribute on the form field it creates to false
    * could allow for a list of non-editable field names to passed in
to the __init__ method of the form to allow for overriding when
creating an instance
  * form widgets can have a render_noneditable method or some other
mechanism that allows them to draw their value for display only which
BoundField's as_widget method would call - this allows for widgets to
control how their value is rendered, for example an email address
widget might show itself as a mailto: link
  * BoundField's as_widget method would change "if not
self.form.is_bound" to "if not form.is_bound or not
self.field.editable" so as to only pick up the value from the initial
data rather than submitted data (as a non-editable field shouldn't
have value in submitted data)
  * non-editable fields wouldn't be processed in the data 'cleaning'
as, again, they shouldn't have values in the submitted data

This would then allow for fields to be selectively read only and still
have their values displayed.

I could, of course, have logic in the form templates instead of the
above which embed the field as a hidden field, displays the read
value, and then further logic in the submit/save handling to ensure a
value change isn't hacked in by the user, but widgets being able to
represent their value for display-only purposes seemed cleaner :)

As I'm new to django I've no idea if it sits well with the thinking
behind django's forms (and there may be major pitfalls with the idea),
but I thought I'd punt it as an idea in case anyone else thinks it's
useful!

Regards,
Matt

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to