Disable field creation in forms for hidden model fields (#3247)

13 views
Skip to first unread message

Philipp Keller

unread,
Jan 18, 2007, 2:59:29 AM1/18/07
to django-d...@googlegroups.com
Hi there

I'm trying to start the design discussion needed for #3247 concerning newforms.

Currently if you add a field to your model with editable=False, e.g.
created = models.DateTimeField(editable=False)
then the field "created" isn't rendered in a form in the admin but it will still be rendered when printing a form transformed from the model by django.newforms.models.form_for_model.

I think it's quite important to have the opportunity to say "I don't want this field to appear in any of my forms" but on the other hand, you may want to suppress this fields in the admin.

I think if I wouldn't know anything of django and would look at editable=False in the model, I'd think it would affect all forms, not just the admin forms.
For the admin, the settings usually go into the admin-metaclass. In fact there is a setting in the admin metaclass to control the fieldSETs but not the appearing fields.

I can think of two possible solutions:

a) have an extra parameter editable_admin=False (although this is quite ugly and isn't backwards compatible)
b) have a new admin metaclass "option" in the form of
class Admin:
editable = [list of all fields to show up in admin]

in case there's no editable option set in the Admin subclass, the fields' editable-parameter is taken into account.

Any thoughts?

greets
Philipp

simonbun

unread,
Jan 19, 2007, 5:17:44 AM1/19/07
to Django developers
I think its a good idea.

If editable=False is specified in the model, then I personally would
expect it not to show up in the form_from_model form either.

On the other hand, I'm not so sure that the need to differentiate the
editable option between the admin contrib and plain form_from_model is
so general. Not for it to warrant an addition to the admin class api at
least. Off the top of my head i can't think of common uses for it, but
feel free to give some examples.
If it were to go in the api in the end, i would prefer it to be
"non_editable = [list of all fields to are not to show up in admin]",
for the sake of brevity, and to still allow an empty admin class.

regards,
Simon

Gary Wilson

unread,
Jan 19, 2007, 11:02:58 AM1/19/07
to Django developers
simonbun wrote:
> If editable=False is specified in the model, then I personally would
> expect it not to show up in the form_from_model form either.
>
> On the other hand, I'm not so sure that the need to differentiate the
> editable option between the admin contrib and plain form_from_model is
> so general. Not for it to warrant an addition to the admin class api at
> least. Off the top of my head i can't think of common uses for it, but
> feel free to give some examples.

Well, I have many forms where there are fields I want to show in the
admin interface, but not on forms users see. A coworker and I recently
built a form-building framework where we resorted to adding a couple
hooks in our view code to post-process the Manipulator/FormWrapper to
not remove certain fields when displaying the form and to post-process
the user's POST data to ensure they cannot put data in those specified
fields.

I do agree that an editable=True/False parameter in the model field
should apply to all forms created from the model. If the admin
interface needs something different, then there needs to be a way to
describe it in class Admin. I think Adrain might have mentioned
something about this with newforms, but I can't recall what or where it
was.

quentinsf

unread,
Jan 20, 2007, 2:17:08 PM1/20/07
to Django developers
Just an extra vote for this.

I I think it's probably neatest to have the option of an editable
parameter in the Admin class, which overrides any model settings.
Perhaps:

class Admin:
editable = {'field1': True, 'field2': False}

Adrian Holovaty

unread,
Jan 22, 2007, 10:12:09 PM1/22/07
to django-d...@googlegroups.com
On 1/18/07, Philipp Keller <philipp...@gmail.com> wrote:
> I think it's quite important to have the opportunity to say "I don't want this field to appear in any of my forms" but on the other hand, you may want to suppress this fields in the admin.
>
> I think if I wouldn't know anything of django and would look at editable=False in the model, I'd think it would affect all forms, not just the admin forms.
> For the admin, the settings usually go into the admin-metaclass. In fact there is a setting in the admin metaclass to control the fieldSETs but not the appearing fields.
>
> I can think of two possible solutions:
>
> a) have an extra parameter editable_admin=False (although this is quite ugly and isn't backwards compatible)
> b) have a new admin metaclass "option" in the form of
> class Admin:
> editable = [list of all fields to show up in admin]

Here's an idea that's a bit cleaner, thanks to the newforms-admin branch...

In that branch, the Admin options class has a new hook,
formfield_for_dbfield(). This takes a database Field instance and
returns the newforms Field instance, so you can use it to implement
admin-specific editable stuff:

def formfield_for_dbfield(self, db_field, **kwargs):
if db_field.name == 'my_uneditable_field':
return None # No form field
return super(MyAdminClass,
self).formfield_for_dbfield(db_field, **kwargs)

The remaining question, then, is whether form_for_model should *not*
create formfields for database fields with editable=False. Judging by
people's comments in this thread, it seems like that's a no-brainer --
if editable=False, then the form_for_model form should not include the
field.

If there aren't any other thoughts on this, let's go ahead and update
the ticket using our slick new triage process. :-)

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com

Reply all
Reply to author
Forward
0 new messages