Search code examples
pythondjangomodelformdjango-2.0manytomanyfield

How to set many-to-many field to current user after saving form


So here's my problem, I'm trying to get the currently logged in user to put in a manytomanyfield field when processing a form.

Models.py

The model contains a field contributors in manytomanyfield

class Property(models.Model):
    title = models.CharField(max_length=250,verbose_name="Nom de l'établissement")
    contributors = models.ManyToManyField(User, verbose_name="Liste des collaborateurs autorisés") # ManyToMany pour autoriser plusieurs "User" à y accéder
    token = models.CharField(max_length=500,verbose_name="Token") # Token généré par nous ou via un app secret key
    payday = models.CharField(max_length=500,verbose_name="Jour de paye", null=True, blank=True)
    planning = models.ImageField('Planning', null=True, blank=True )
    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('property-home')

For the creation of a property I simply display the title field in the model form.

class CreateProperty(forms.ModelForm):
    class Meta:
        model = Property
        fields = ['title',]

And processing the form (views.py)

I'm trying to add the id of the currently logged in user via request.user.pk

    def create_property(request):
    if request.method == "POST":
        form = CreateProperty(request.POST)
        if form.is_valid():
            instance = form.save(commit=False)
            instance.token = 'TOKEN_GENERATOR_SIMULATION'
            instance.contributors = request.user.pk
            instance.save()
            messages.success(request, _(f"good"))
            return redirect('create-property')
        else:
            messages.error(request, _(f"The form is invalid"))
            return HttpResponseRedirect(reverse('create-property'))
    else:
        form = CreateProperty(instance=request.user) 
        form.contributors = request.user
        args = {'form': form}
        return render(request, 'create_property.html', args)

But when I try to create a property I come across an error:

TypeError at /property/create
Direct assignment to the forward side of a many-to-many set is prohibited. Use contributors.set() instead.
Request Method: POST
Request URL:    http://localhost:8000/property/create
Django Version: 2.1.7
Exception Type: TypeError
Exception Value:    
Direct assignment to the forward side of a many-to-many set is prohibited. Use contributors.set() instead

What I want is for a user to create a property, it is directly associated.


Solution

  • As the error says, you can use contributers.set() instead of assigning to it. You need to do this after the instance has been saved.

    instance.save()
    instance.contributors.set([request.user.pk])
    

    See the docs on many-to-many relationships for more examples.