[Answered ]-Request.POST used by ModelForm with prefix not working with client.post in unit test

2👍

You can take the values from the initial dict and add a prefix manually:

data = {"{}-{}".format(prefix, k): v for k, v in customer_form.initial.items()}

0👍

I found the solution for this problem here on this old discontinued post: https://code.djangoproject.com/ticket/13763. I don’t know if this is the most elegant way around it, but it was the easiest way for me. Basically you have to put the prefix right before the field name. So the test structure may become something like:

data = {'YOURprefix-fieldName':'anything'}

response = self.client.post(reverse('url_name', kwargs{'foo','bar'}), data)

That way, your views.py is_valid() check bellow will return true (if the data put into the form is valid of course):

form = MyForm(request.POST, prefix=YOURprefix)
if form.is_valid():
...

Leave a comment