Django Error Management_Form

Dear all,

I am begginer on Django and I need some help :slight_smile:

On my model I want to create an object “request” and some objects “delivery”. A “request” can have multiple “deliveries”.

Here is my model :

class Deliveryrequest(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
deliverydate = models.DateField()

class Delivery(models.Model):
deliveryrequest = models.ForeignKey(Deliveryrequest, on_delete=models.CASCADE)
adresse = models.ForeignKey(Adresse, on_delete=models.CASCADE)
statut = models.ForeignKey(Statut, on_delete=models.CASCADE)
nbtodeliver = models.IntegerField(choices=list(zip(range(0, 10), range(0, 10))), unique=False)
nbtorecover = models.IntegerField(choices=list(zip(range(0, 10), range(0, 10))), unique=False)

Here is my :

class DeliveryrequestForm(forms.ModelForm):
class Meta:
model = Deliveryrequest
fields = (‘deliverydate’,)
widgets = {
‘deliverydate’: forms.DateInput(format=(’%d-%m-%Y’), attrs={‘firstDay’: 1, ‘pattern=’: ‘\d{4}-\d{2}-\d{2}’, ‘lang’: ‘fr’, ‘format’: ‘yyyy-mm-dd’, ‘type’: ‘date’}),
def init(self, *args, **kwargs):
super(RequestForm, self).init(*args, **kwargs)
self.fields[‘deliverydate’].label = “Delivery Date”

class DeliveryForm(forms.ModelForm):
class Meta:
model = Delivery
fields = (‘adresse’,‘nbcolisalivrer’, ‘nbcolisarecuperer’,)
def init(self, *args, **kwargs):
self._user = kwargs.pop(‘user’)
super(DeliveryForm, self).init(*args, **kwargs)
self.fields[‘adresse’].queryset = Adresse.objects.filter(user=self._user)
self.fields[‘nbtodeliver’].label = "Nombre de colis à livrer "
self.fields['nbtorecover '].label = “Nombre de colis à récupérer”

Here is my view to create these objects throw form (Deliveryrequest) and formset (Delivery) :

def newdeliveryrequest(request):
adresse = Adresse.objects.filter(user=request.user)
max = len(adresse)
formDeliveryrequest = DeliveryrequestForm(initial={‘deliverydate’:})
DeliveryrequestFormSet = formset_factory(wraps(DeliveryForm)(partial(DeliveryForm, user=request.user)), max_num=max)
formsetDelivery = DeliveryFormSet(initial=[{‘adresse’: x, ‘nbcolisalivrer’: ‘0’, ‘nbcolisarecuperer’: ‘0’} for x in adresse])
if request.method == “POST”:
formDeliveryrequest = DeliveryrequestForm(request.POST)
formset = DeliveryFormSet(request.POST)
if formDeliveryrequest .is_valid():
deliveryrequest =
deliveryrequest .user = request.user
deliveryrequest .save()
for form in formset:
if form.is_valid():
delivery.deliveryrequest = deliveryrequest
statut = Statut.objects.get(name=“Demande envoyée”)
delivery.statut_id = statut
return render(request, ‘livraison/mesdemandes.html’)
return render(request, ‘livraison/nouvelledemande.html’, {‘formDemande’: formDemande, ‘formLivraison’: formsetLivraison})

When saving my form I get this error :

ManagementForm data is missing or has been tampered with

ValidationError at /livraison/mesdemandes/new/

[‘Les données du formulaire ManagementForm sont manquantes ou ont été manipulées’]

Request Method: POST
Request URL: http://localhost:8000/livraison/mesdemandes/new/
Django Version: 3.1.6
Exception Type: ValidationError
Exception Value: [‘Les données du formulaire ManagementForm sont manquantes ou ont été manipulées’]
Exception Location: C:\Users\Nizard.MELLITI\AppData\Local\Programs\Python\Python39\lib\site-packages\django\forms\, line 92, in management_form
Python Executable: C:\Users\Nizard.MELLITI\AppData\Local\Programs\Python\Python39\python.exe
Python Version: 3.9.1
Python Path: [‘C:\Users\Nizard.MELLITI\Desktop\mysite’, ‘C:\Users\Nizard.MELLITI\AppData\Local\Programs\Python\Python39\’, ‘C:\Users\Nizard.MELLITI\AppData\Local\Programs\Python\Python39\DLLs’, ‘C:\Users\Nizard.MELLITI\AppData\Local\Programs\Python\Python39\lib’, ‘C:\Users\Nizard.MELLITI\AppData\Local\Programs\Python\Python39’, ‘C:\Users\Nizard.MELLITI\AppData\Local\Programs\Python\Python39\lib\site-packages’]
Server time: Tue, 06 Apr 2021 21:01:07 +0200

I tried many things but nothing works. I really need your help. Many thanks in advance for all :slight_smile:

Kind Regards,

When you use a formset, there’s an attribute named management_form that must be rendered. This message is usually an indication that you have not included it in your template.

See the docs on Understanding the ManagementForm.

If you need more specific information, please post your template being used for this page.

Also, when you post code or templates here, please enclose it between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This allows the forum software to keep the proper formatting of your code, which is critical in Python.

Many thanks for the help it works great ! Many thanks also for the advice to post code and templates :slight_smile: