My advice is to use custom model form and hide these M2M fields if you are
creating a new object.
You can overwrite the init method of the form to do this.

On Mon, Sep 12, 2011 at 6:01 PM, Carlos Brum <carlos...@gmail.com> wrote:

> Hello guys,
>
> I have a problem to deal with a field ManyToMany in Django. It is
> the following:
>
> When I try to access a list of relations that is my ManyToMany field
> I get an error
>
> "%ModelName% instance needs to have a primary key value before a many-
> to-many relationship Can be used. "
>
> I need to perform validation "before storing the record in the
> database".
> As you can see in the "view" code below, I used "save" in the
> "ModelForm" with "commit = False"
> so would build the ManyToMany relationship  in memory.
>
> This did not work.
>
> I've tried to find the solution on the internet but found nothing that
> work.
>
> Surely someone has been there.
>
> Thanks to collaboration.
>
> ######################################
> # Source
> ######################################
> Note: The Model "Group" is the native Django admin
>
> ######################################
> # Model
> ######################################
>
> class Application (models.Model):
>
>   . . .
>
>   groups =
> models.ManyToManyField (Group related_name = 'application')
>
>   . . .
>
>   eh_valida def (self):
>
>       validating = True
>
>       # ######################################
>       # HAVE PROBLEMS HERE. The attribute "self.grupos" is not
> filled. It should be a list.
>       # Error: %ModelForm% instance
>       # Needs to have a primary key value before the
>       # Many-to-many relationship Can be used. "
>       # ######################################
>
>       meuConjuntoDeGrupos = set (self.grupos)
>
>       # ######################################
>       . . .
>
>       return validating
>
> ######################################
> # ModelForm
> ######################################
> AplicacaoForm class (ModelForm):
>
>   class Meta:
>       model = Application
>
>
> # View
>
> def adiciona_aplicacao (request, idQuestionario):
>
>   form = AplicacaoForm (request.POST)
>
>   if request.method == 'POST':
>
>       if form.is_valid ():
>
>           form.save (commit = False)
>
>           if form.instance.eh_valida ():
>
>               form.save (commit = True)
>               HttpResponseRedirect return (reverse (manager))
>           #
>
>   . . .
>
> --
> 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.
>
>


-- 
eng. Ilian Iliev
Web Software Developer

Mobile: +359 88 66 08 400
Website: http://ilian.i-n-i.org

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