Unfortunately, this doesn't work without a primary key needed for many-to-
many relations that are used in the model.

Previous tip with modifying base fields before instantiating a form 
object works better.


On Tue, 24 Jul 2007 22:37:06 +0000, Patrick wrote:

> Seems like an elegant and logical solution. Thanks, Michael!
> 
> On Tue, 24 Jul 2007 21:04:50 +0000, Michael wrote:
> 
>> When I came across the same issue (model default values not being
>> selected), I simply stopped using form_for_model for new forms and
>> instead created an instance of my model in memory then used
>> form_for_instance... for eg:
>> 
>> p = Post()
>> PostForm = form_for_instance(p)
>> 
>> That way the default values for the model are set when the new object
>> is created.
>> 
>> Hope that's relevant to your situation... not 100% sure.
>> 
>> On Jul 25, 6:19 am, Patrick <[EMAIL PROTECTED]> wrote:
>>> On Tue, 24 Jul 2007 11:51:36 -0700, Doug B wrote:
>>> > I don't know how others have approached it, but I have a 'settings'
>>> > file with defaults defined in one place and reference those values
>>> > via imports in the form file and model file.  For values specific
>>> > for the app, I stick them in the models file.
>>>
>>> > models.py
>>> > ---------
>>> > POST_DEFAULTS = {'status':'published'}
>>>
>>> > class Post(models.Model):
>>> >         status  = models.CharField(
>>> >                         maxlength = 15,
>>> >                         choices = PUBLICATION_STATUS,
>>> >                         default = POST_DEFAULTS['status'])
>>>
>>> > forms.py
>>> > ---------
>>> > from app.models import POST_DEFAULTS,PUBLICATION_STATUS
>>>
>>> > class PostForm(forms.Form):
>>> >     status =
>>> > forms.CharField(forms.CharField(widget=forms.Select
>>>
>>> (choices=PUBLICATION_STATUS,
>>>
>>> > initial = POST_DEFAULTS['status'])
>>>
>>> > ---or--- if you are doing form_for_* (I don't use those, but this
>>> > should be close)
>>>
>>> > views.py
>>> > ---------
>>> > PostForm = form_for_model(Post)
>>> > PostForm.base_fields['status'].initial = POST_DEFAULTS['status']
>>> > form = PostForm()
>>>
>>> > If you use the helpers, the important thing to remember is to modify
>>> > the base_fields dict before instantiating the form.  Yet another
>>> > option, is to create a callback function passed to form_for_model.
>>> > The callback function basically gets called for every field in the
>>> > model and you have the choice of making changes for each field. 
>>> > That method always felt cumbersome compared to just changing the
>>> > values you need changed, so I can't do it off the top of my head.  A
>>> > search for formfield callback should tell you how though.
>>>
>>> Thanks for this tip, Doug. It works.
>>>
>>> I somehow missed it in the docs.
>> 
>> 
>> 
>> 
> 
> 


--~--~---------~--~----~------------~-------~--~----~
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