#4312: addition of defaults argument to newforms save()
----------------------------------+-----------------------------------------
Reporter: [EMAIL PROTECTED] | Owner: adrian
Status: new | Component: django.newforms
Version: SVN | Resolution:
Keywords: | Stage: Design decision
needed
Has_patch: 1 | Needs_docs: 0
Needs_tests: 0 | Needs_better_patch: 0
----------------------------------+-----------------------------------------
Comment (by anonymous):
Replying to [comment:1 Simon G. <[EMAIL PROTECTED]>]:
> Hmm.. what benefits does this have over using initial values?
I'm not sure that "defaults" was the right terminology for this. This was
not intended to provide an initial value for the Form field or a default
value for the Model attribute. Instead, this was meant as a "hook" that
would allow the developer to supply values for Model attributes that are
not intended to be a form field (neither hidden nor visible)--usually
fields that are not "editable" or are not included in the "fields"
argument to newforms.models.form_for_instance() or
newforms.models.form_for_model().
A better example might be something like this:
{{{
from django.db import models
from django.contrib.auth.models import User
class Blog(models.Model):
name = models.CharField(maxlength=16)
class Entry(models.Model):
blog = models.ForeignKey(Blog, editable=False)
user = models.ForeignKey(User, editable=False)
text = models.TextField()
}}}
When an Entry is created, the blog and user attributes are required, but
they won't be part of the form--they must be supplied by some other means
(e.g., the session, URL, or query string). Then they are added to the
instance by passing them into the "defaults" argument of the Form's save
method. This way save(False) doesn't need to be called before the
attributes are set. In this case it's mostly needed when creating the
instance. If there is many_to_many data, then that would also have to be
handled somehow (see docs for save_instance()).
There may be other ways to do this, but I found this to be backwards
compatible and relatively simple solution...
--
Ticket URL: <http://code.djangoproject.com/ticket/4312#comment:2>
Django Code <http://code.djangoproject.com/>
The web framework for perfectionists with deadlines
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" 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-updates?hl=en
-~----------~----~----~----~------~----~------~--~---