Django update m2m field

I have a Model Service and a ModelForm named Service that I use to add and update a service model. The model looks like this:

class Service(models.Model):
    categories = models.ManyToManyField(Category)

      

The category field is displayed as a multi-selectable tag. It works well when I add a new entry, but when I update it, only one service appears per request. POST ['categories'] even if I selected multiple categories.

I tried to dump the request object and I can see that the categories show something like:

u'categories': [u'3', u'4', u'2']

      

I tried to call request._get_post () and it only returned 1 category, so request.POST ['categories'] only returns 1. Who knows what's going on and how to fix it?

+2


a source to share


1 answer


You might want to use

request.POST.getlist('categories')

      



which will return all selected values โ€‹โ€‹for that form field.

+6


a source







All Articles