On Tue, Feb 8, 2011 at 10:21 AM, NavaTux <navaneetha...@gmail.com> wrote: > Hi Django Users, > I had done the custom form with some fields which > includes(charfield,foreignkey(ModelChoiceField),many_to_many > field(MultiplemodelChoiceField) ),I had created a formset for my form then i > need to process and pass the formset datas into my template > this is my form: > class JobPostingForm(forms.Form): > name = forms.CharField(max_length=250) > city = forms.ModelChoiceField(queryset=City.objects.all()) > tag = forms.ModelMultipleChoiceField(queryset=Tag.objects.all()) > (Formset) > MJobFormSet=formset_factory(JobPostingForm, max_num=5, extra=2) > > view function > def view_post_multiple_jobs(request,post_mjobs_template): > if request.method == 'POST': > mjob_form_set = MJobFormSet(request.POST) > if mjob_form_set .is_valid(): > form_set_data = > mjob_form_set.cleaned_data > > def _save_job(job): > name=job['name'] > city=______ > tag=______ > > mjob=Job.objects.create(name=name,city=city,tags=tag) > return mjob > map(_save_job, [job for job in form_set_data if job] > return (template) > Here I don't know that how to process the city,tag objects in _save_job() > because city is foreign key object and tag is many_tomany object retirned > from formset > could you please point it even i have tried in times >
1) Use a ModelForm, which would do this for you. [1] 2) M2Ms aren't created when you create the initial instance of an object, you must first create the object, and then relate it. Semantically, creating an M2M link between two objects is creating a row in a links table between them, with the row containing the primary key of one object and the primary key of the other object. Therefore, both objects must exist in the database before you can link them, and your question is simply "How do I create an M2M link between two objects", which is well answered in the docs [2]. Cheers Tom [1] http://docs.djangoproject.com/en/1.2/topics/forms/modelforms/ [2] http://docs.djangoproject.com/en/1.2/ref/models/relations/ -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.