On 12/22/07, Yatla <[EMAIL PROTECTED]> wrote:
>
> I need to initialize a selection in a M2M field of the Document
> ModelForm during the first GET of a form that creates a new Document -
> classes and view as follows:
>
> class DocumentForm(forms.ModelForm):
>     class Meta:
>         model = Document
>
> class Document(models.Model):
>     .......
>     categories = models.ManyToManyField(Category)
>
> view function:
> def create(request, category_name, template='edit2.html', params={}):
>     category = get_object_or_404(Category, name=category_name)
>     new_doc = Document(author=request.user, pub_date=datetime.now())
>     if request.method == 'POST':
>         form = DocumentForm(request.POST, instance=new_doc)
>         if form.is_valid():
>             .....
>     else:
>         form = DocumentForm(instance=new_doc)  # todo: figure out how
> to initialize categories
>     params['form'] = form
>     return render_to_response(template, params,
> RequestContext(request))

Are you trying to assign all new docs to an existing Category object
then? I think if you used the 'initial' argument in in the form
constructor it would work. Something like:

    initial_data={'categories': [category.id]}
    form = DocumentForm(request.POST, initial=initial_data, instance=new_doc)

Joseph

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