Hi Furbeenator,
thanks a lots, it works !!! :-)))

I've use your first solution. You say that "category would have to be
the first Allow Null field in your model" wich is not the case here. A
"phone" field is the first step (Step0) can be empty :
telephone = models.CharField(max_length=14, blank=True)

Maybe I do a confusion between "Allow Null" and "blank=True) ?


To be sure to understand your solution, I go bellow step by step:

1 class InscriptionWizard(SessionWizardView):
2    def done(self, form_list, **kwargs):
3        instance = Customer()
4        for form in form_list:
5            for field, value in form.cleaned_data.iteritems():
6                if field == 'category':
7                    instance.save()
8                setattr(instance, field, value)
9        instance.save()

Line 5 : Iterate over field and values
Line 6 : If field name is category, then save the instance BEFORE
setting attribute (setattr) for the category field. Then a PK is
available in the DB for saving the m2m field category, wich is done
Line 9.

Is it the way it works and has to works ?


Again, thanks a lots for help .... Yahoooooo !!!

Regards

Alain







On 2 nov, 19:03, Furbee <furbeena...@gmail.com> wrote:
> In your Customer model there are fields which cannot be Null, so you cannot
> instance.save() before setting those properties. So, you may have to check
> for the category field in your loop and if it is category, save the
> instance first. Something like the following:
>
> views.py (in clients application)
> ---------------------------------------------
> class InscriptionWizard(SessionWizardView):
>    def done(self, form_list, **kwargs):
>        instance = Customer()
>        for form in form_list:
>            for field, value in form.cleaned_data.iteritems():
>                if field == 'category':
>                    instance.save()
>                setattr(instance, field, value)
>        instance.save()
> ---------------------------------------------
>
> This would of course depend on your ordering of the fields. In other words,
> category would have to be the first Allow Null field in your model, because
> you cannot save() until all Not Null fields have a value. If you cannot
> order these fields so the category is last, you may have to do something
> like:
>
> views.py (in clients application)
> ---------------------------------------------
> class InscriptionWizard(SessionWizardView):
>    def done(self, form_list, **kwargs):
>        instance = Customer()
>        for form in form_list:
>            for field, value in form.cleaned_data.iteritems():
>                if field != 'category':
>                    setattr(instance, field, value)
>        instance.save()
>        for form in form_list:
>            for field, value in form.cleaned_data.iteritems():
>                if field == 'category':
>                    setattr(instance, field, value)
> ---------------------------------------------
>
> I'm sure there is a MUCH more elegant way of getting just the categories
> from the forms and field/value pairs, this is brute force (not very
> scalable for large number of fields), but should work. Anybody else please,
> show code that could get the specific forms and field/values for categories.
>
> Furbeenator
>
>
>
>
>
>
>
> On Wed, Nov 2, 2011 at 10:28 AM, youpsla <youp...@gmail.com> wrote:
> > Hello,
> > i'm currently doning a website where user can register (without
> > password, without auth module of Django). They put some informations
> > and at the end (Step5Form) do multiple choices by clicking on
> > checkboxes. When I click on validate on the last step I've "instance
> > needs to have a primary key value before a many-to-many relationship
> > can be used" error. I've search the web to find a solution but ..... :-
> > (
>
> > Here is my code:
>
> > models.py for Customer (in clients application)
> > ---------------------------------------------
> > class Customer (models.Model):
>
> >    email_adresse = models.EmailField(max_length=255, unique=True)
> >    some fields ..... for Step2 to Step4 of the form
> >    category = models.ManyToManyField(Categories)
> > ---------------------------------------------
>
> > models.py for categories (in categories application)
> > ---------------------------------------------
> > class Categories (models.Model):
> >    category = models.CharField(max_length=200)
>
> >    def __unicode__(self):
> >        return unicode(self.category)
> > ---------------------------------------------
>
> > The ManyToMany field has created(syncdb) a table
> > clients_customer_category with the following columns:
> >        1       id                      int(11)
> > AUTO_INCREMENT
> >        2       customer_id     int(11)
> >        3       categories_id   int(11)
>
> > urls.py
> > ---------------------------------------------
> > urlpatterns = patterns('',
> >    (r'^clients/$', InscriptionWizard.as_view([Step1Form, Step2Form,
> > Step3Form, Step4Form, Step5Form])),
> > )
> > ---------------------------------------------
>
> > forms.py (in clients application)
> > ---------------------------------------------
> > from django import forms
> > from clients.models import Customer
> > from categories.models import Categories
>
> > class Step1Form(forms.Form):
> >    email_adresse = forms.EmailField(max_length=255)
>
> > class Step5Form(forms.Form):
> >    category =
> > forms.ModelMultipleChoiceField(queryset=Categories.objects.all(),
> > widget=forms.CheckboxSelectMultiple, required=True)
>
> > views.py (in clients application)
> > ---------------------------------------------
> > class InscriptionWizard(SessionWizardView):
> >    def done(self, form_list, **kwargs):
> >        instance = Customer()
> >        for form in form_list:
> >            for field, value in form.cleaned_data.iteritems():
> >                setattr(instance, field, value)
> >        instance.save()
> > ---------------------------------------------
>
> > I got the following error:
> > ---------------------------------------------
> > Request Method: POST
> > Request URL:    http://127.0.0.1:8010/clients/
> > Django Version: 1.3.1
> > Exception Type: ValueError
> > Exception Value:
> > 'Customer' instance needs to have a primary key value before a many-to-
> > many relationship can be used.
> > ...............
> > ...............
> > ▶ Local vars
> > C:\dev\Flash\clients\views.py in done
> >                setattr(instance, field, value) ...
> > ---------------------------------------------
>
> > What I've tried:
> > 1)I've search the web and didn't find any way to solve this. I've read
> > on save_m2m() wich doesn't apply here as far I understand because no
> > "commit=False" and this form is not a ModelForm.
>
> > 2)I've trie to save in 2 steps with the hope an id has been set in the
> > DB:
> > -First step : for form in form_list[0:3]: ...... instance.save()
> > -Second step :
> >        for form in form_list[4]:
> >            for field, value in form.cleaned_data.iteritems():
> >                instance.category.add(value)
>
> > I've the following error : 'BoundField' object has no attribute
> > 'cleaned_data' for the line "for field, value in
> > form.cleaned_data.iteritems():" in Second step.
>
> > Then I can maybe split my form in 2 forms, one from Step0 to Step3 and
> > another one for Step4 and passing the id_customer in teh session but
> > maybe there is a solution to solve this issue in one form.
>
> > Help will be really appreciated.
>
> > I apologize if my explanations are not enough clear (and for my
> > english) and feel free to ask informations.
>
> > Regards
>
> > Alain
>
> > --
> > 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.

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

Reply via email to