How are default values for fields used

9 views
Skip to first unread message

akaihola

unread,
Jan 12, 2006, 5:21:55 AM1/12/06
to Django users
In the admin interface, I can't omit fields from "admin =
meta.Admin(fields=...)" even if they do have default values. If I do
that, I get errors for required fields.

In my own forms, I first assumed I could just leave out fields with
default values in the model, but my form.error_dict gets filled with
complaints of missing required values. I don't want the default values
to go through the form as hidden fields.

So, if I'm not mistaken, the default values for a model's fields are
not used when saving an object, and I must create a custom manipulator
to do that. But is there a reason why this isn't the current behavior?

Maniac

unread,
Jan 12, 2006, 7:11:16 AM1/12/06
to django...@googlegroups.com
akaihola wrote:

>So, if I'm not mistaken, the default values for a model's fields are
>not used when saving an object,
>

They are used to prefill form elements in the Admin (don't know about
custom forms though, never tried). So it's just a hint for the user.

> and I must create a custom manipulator
>to do that.
>

Not necessarily. It's more convinient to override _pre_save() method in
your model and fill default values there.

akaihola

unread,
Jan 12, 2006, 2:57:43 PM1/12/06
to Django users
Ok, I'll take a look at the _pre_save() mechanism. By the way, isn't
that one of the things changing in magic-removal branch?


I still wonder about the default values question though.

I tried to create a Django object in an interactive Python shell, and
the fields are indeed filled with default values. So somewhere in the
form/manipulator mechanism there are unnecessary checks which prevent
this from happening and raise errors instead.

I'd like to research and maybe make a patch for this unless the current
behavior is intended with a good reason.

akaihola

unread,
Jan 12, 2006, 3:19:14 PM1/12/06
to Django users
It seems that _pre_save() is never called if the manipulator catches
missing fields. So customizing the manipulator seems to be my quick fix
after all.

I also checked the manipulator objects' attributes, and default values
defined in models don't even end up there. So it might require quite a
few changes to have the object save functionality work as I vision.

One more thought: In RDBM world, a DEFAULT attribute for a column makes
the database engine fill in default values whenever fields are missing
from INSERT statements. So it would be kind of intuitive to do the same
wrt Django manipulators.

Maniac

unread,
Jan 12, 2006, 5:49:44 PM1/12/06
to django...@googlegroups.com
akaihola wrote:

>Ok, I'll take a look at the _pre_save() mechanism. By the way, isn't
>that one of the things changing in magic-removal branch?
>
>

Yes, it goes away. Instead one would be able to override save() and do
everything in it.

>I still wonder about the default values question though.
>
>I tried to create a Django object in an interactive Python shell, and
>the fields are indeed filled with default values. So somewhere in the
>form/manipulator mechanism there are unnecessary checks which prevent
>this from happening and raise errors instead.
>
>

Indeed, default values are set upon model object creation. This is good
:-). But a manipulator doesn't prevent them in any way. What happens is
that a manipulator checks the values that come from the web, and not the
default ones inside it.

To make it work as expected you do something like:

m=someitems.AddManipulator() # or someitems.ChangeManipulator(id)
data=m.flatten_data() # this returns object's data suitable for
rendering on the web including default values
f=formfields.FormWrapper(m, data, {}) # form gets values from this
'data' parameter

And then you form will be prefilled. And you can feed these values to
the saving manipulator on POST

Now that makes me wonder why data is passed to a form separately from
manipulator which has it anyway...

akaihola

unread,
Jan 13, 2006, 3:14:26 AM1/13/06
to Django users
Maniac wrote:
>What happens is that a manipulator checks the values that come from the web, and not the
default ones inside it.

Actually, the manipulator doesn't even know about the default values of
the model.

Your example is based on hidden fields with default values. However,
I've understood that it's considered bad practice [1].

What I ended up with is this quick-and-dirty solution:

if request.POST:
new_data = request.POST.copy()
for field in MyModel._meta.fields:
if field.name not in new_data.keys():
if field.default != NOT_PROVIDED:
# NOT_PROVIDED imported from
django.core.meta.fields
new_data[field.name] = str(field.default)
else:
new_data[field.name] = ''
new_task_data['mydatetimefield_date'] = ''
# DateTimeFields must be handled separately
errors = manipulator.get_validation_errors(new_data)

I'm not sure what kind of trouble I'm putting myself into since the
default values come from Python and go to HTML POST data before
html2python. I guess at least boolean fields need special treatment.

There was discussion about manipulators and default values on the irc
channel yesterday [2]. I also made a ticket on Jacob's request [3].

[1]
http://code.djangoproject.com/wiki/CookBookManipulatorWithHiddenFields
[2] http://simon.bofh.ms/logger/django/2006/01/12/ from 15:10 to 15:21
[3] http://code.djangoproject.com/ticket/1207

Maniac

unread,
Jan 13, 2006, 5:11:54 AM1/13/06
to django...@googlegroups.com
akaihola wrote:

>Actually, the manipulator doesn't even know about the default values of
>the model.
>
>

Why that? If you just create AddManipulator and get its flatten_data
you'll get default values of the model.

>Your example is based on hidden fields with default values. However,
>I've understood that it's considered bad practice [1].
>
>

No, I didn't suggest using hidden fields. I suggested normal visible
fields prefilled with default values.

If you don't want to give user a chance to edit them then yes it would
be better not to expose them in forms at all and just set them manually
before save.

You also can make a default manipulator not complain about those fields
being absent on validation. Give them "editable=False" in the model.
Then manipulator will omit them.

Jeffrey E. Forcier

unread,
Jan 13, 2006, 10:45:17 AM1/13/06
to django...@googlegroups.com
On Jan 13, 2006, at 5:11 AM, Maniac wrote:
>
> You also can make a default manipulator not complain about those
> fields being absent on validation. Give them "editable=False" in
> the model. Then manipulator will omit them.

Assuming this is still the same issue aikihola brought up on IRC
yesterday, setting editable=False won't work, as he *does* want those
fields to be editable in other templates, just not the one in question.

Regards,
Jeff

--
Jeffrey E. Forcier
Research and Development
Stroz Friedberg, LLC
15 Maiden Lane, 12th Floor
New York, NY 10038
[main]212-981-6540 [direct]212-981-6546
http://www.strozllc.com

This message is for the named person's use only. It may contain
confidential, proprietary or legally privileged information. No right to
confidential or privileged treatment of this message is waived or lost
by any error in transmission. If you have received this message in
error, please immediately notify the sender by e-mail or by telephone at
212.981.6540, delete the message and all copies from your system and
destroy any hard copies. You must not, directly or indirectly, use,
disclose, distribute, print or copy any part of this message if you are
not the intended recipient.


akaihola

unread,
Jan 13, 2006, 6:09:17 PM1/13/06
to Django users
Jeff's right, I want to edit all fields in the full edit form, and use
the defaults only in a quick add form.

I generalized the code I showed above to a fill_missing_fields
function. Now the only remaining problem I see is that it has to be
called before validation and html2python. It would be neater to have
validation bypass missing fields with default values and have them
filled in after html2python.

Reply all
Reply to author
Forward
0 new messages