> Hi,
>
> I created a PostForm, ModelForm for Post model that has a ManyToMany
> relationship with a model called Test, I use a CharField and a
> clean_test(): to convert the comma separated list of test names to a
> list of Test objects and this works perfectly.
Err... How do this work for not yet existing Test instances ?
> Now, when I want to use the PostForm for editing a Post I want the
> CharField to display a comma separated list of test names from the
> ManyToMany field. I can't find where in my PostForm I could hookup to
> fill up the CharField when I do PostForm(instance=instance) to get my
> ManToMany converted to a list of names.
the __init__ method looks like the right place to handle this. First
make sure you exclude the related field from the ModelForm (cf the doc
for ModelForms on how to do this) and add your own field to the form
definition, and make sure you call on the supercall __init__ before
you populate the field.
> Apart from that, is it good manners to access and create other Model
> objects from ModelForms?
* "access" : in the __init__ method.
* "create" : in the save method
> like in clean_ methods?
*don't*. The clean methods are about user inputs validation, and
should be idempotent (ie: should not alter the database state).
--
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 django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
No comments:
Post a Comment