Hello,
I have a challenge using the modelformset_factory module ( https://docs.djangoproject.com/en/3.2/topics/forms/modelforms/) and invite your advice. I can automatically generate a pre-populated webpage with a select dataset from the database, but the generated template creates two sets of fields on my form. One entry has the pre-populated dataset, while the second set is blank. The challenge is any attempt to update the retrieved dataset, leads to the creation of a new (*unnecessary*) entry in my database. Any assistance on how to resolve this is most welcome. Details of my setup are highlighted below: A) PIP FREEZE (tickets) michael@Data-Wrangler tickets % pip freeze asgiref==3.5.2 backports.zoneinfo==0.2.1 certifi==2022.6.15 charset-normalizer==2.0.12 crispy-bootstrap5==0.6 Django==3.2.13 django-cors-headers==3.10.1 Django-crispy-forms==1.14.0 django-webpack-loader==1.6.0 djangorestframework==3.13.1 idna==3.3 image==1.5.33 importlib-resources==5.4.0 pdfkit==1.0.0 phonenumbers==8.12.51 Pillow==9.2.0 PyQRCode==1.2.1 pytz==2022.1 requests==2.27.1 six==1.16.0 sqlparse==0.4.2 typing_extensions==4.1.1 urllib3==1.26.9 uWSGI==2.0.20 zipp==3.6.0 (tickets) michael@Data-Wrangler tickets % B) VIEW @login_required def eventupdate(request, event_id): try: status = ClientProfile.objects.filter(username=str(request.user).strip()). values('operational') except: status = False if (status[0]['operational']) != True: return render(request, 'website/dashboard.html') context = {} ProductFormSet = modelformset_factory(Product, fields = ('name', 'image', 'visible' , 'brief', 'details', 'venue', 'date', 'time', 'promoters', 'advance_ticket', 'gate_ticket','regular_ticket', 'vip_ticket', 'under18_ticket',)) if request.method == 'POST': formset = ProductFormSet(request.POST, request.FILES, queryset =Product.objects.filter(pk=int(event_id))) if formset.is_valid(): formset.save() return HttpResponseRedirect(reverse('website-store')) else: formset = ProductFormSet(queryset=Product.objects.filter(pk=int(event_id))) return render(request, template_name='website/eventupdate.html', context={ 'formset': formset, 'status': status, 'event': event_id }) 1. TEMPLATE {% extends "website/base_portal.html" %} {% block title %} EVENT UPLOAD - EVENTS MAURITIUS {% endblock %} {% block content %} {% load crispy_forms_tags %} {% load static %} <div class="search"> <div class="search-update px-5"> <div class="box-element border-0 rounded-lg mt-0 px-5 py-4"> <h4 class="text-center">UPDATE AN EXISTING EVENT</h4> <form method="post" enctype="multipart/form-data" name="eventUpload"> {% csrf_token %} {{ formset | crispy }} <div class="validation-feedback" id="event-validation"></div> <button class="btn btn-primary mx-auto rounded-pill btn-checkout" type= "submit" id="submitEventProfile">Update Event Details</button> </form> </div> </div> </div> </div> <script type="text/javascript"> const event = {{event}} //document.getElementsByClassName("multiField").style.display = "hidden"; //document.getElementByClassName("multiField").style.visibility = "none"; //$("multiField").remove(); </script> {% endblock %} 1. MODEL class Product(models.Model): name = models.CharField(max_length=1000 , blank=True, null=True, help_text="Full name of the event") price = models.DecimalField(max_digits=7, decimal_places=2, default=0) digital = models.BooleanField(default=False, null=True, blank=True) image = models.ImageField(upload_to='store/', null=True, blank=True) portfolio = models.ForeignKey(ProductImage, on_delete=models.SET_NULL, null= True, blank=True) #category = models.ForeignKey(ProductCategory, on_delete=models.SET_NULL, null=True, blank=True) #tag = models.ManyToManyField(ProductTag) venue = models.CharField(max_length=200, blank=True, null=True) promoters = models.CharField(max_length=200, blank=True, null=True) brief = models.CharField(max_length=200, blank=True, null=True, help_text="A brief description of the event") details = models.TextField(blank=True, null=True, help_text="Separate each line with a fullstop and leave the last line empty") advance_ticket_deadline = models.PositiveIntegerField(default=0) advance_ticket = models.PositiveIntegerField(default=0) gate_ticket = models.PositiveIntegerField(default=0) regular_ticket = models.PositiveIntegerField(default=0) vip_ticket = models.PositiveIntegerField(default=0) under18_ticket = models.PositiveIntegerField(default=0) date = models.DateField(blank=False, null=False, default=timezone.now) time = models.CharField(blank=True, null=True, max_length=20) visible = models.BooleanField(default=False, help_text="Select this field to activate this event on the home page") def __str__(self): return self.name class Meta: ordering = ['date'] dayOfWeek = {0:'MON', 1:'TUE', 2:'WED', 3:'THU', 4:'FRI', 5:'SAT', 6:'SUN'} monthOfYear = {1:'JAN', 2:'FEB', 3:'MAR', 4:'APR', 5:'MAY', 6:'JUN', 7:'JUL', 8:'AUG', 9:'SEP', 10:'OCT', 11:'NOV', 12:'DEC'} @property def imageURL(self): try: url = self.image.url 1. DUPLICATED HTML <div class="multiField"> <div id="div_id_form-0-name" class="mb-3"> <label for="id_form-0-name" class ="form-label"> Name </label> <input type="text" name="form-0-name" value="BERNACHE BEACH SHOW" maxlength="1000" class="textinput textInput form-control" id="id_form-0-name "> <small id="hint_id_form-0-name" class="form-text text-muted">Full name of the event</small> </div> <div id="div_id_form-0-image" class="mb-3"> <label for="id_form-0-image" class="form-label"> Image </label> <div class=" mb-2"> <div class="input-group mb-2"> <span class=" input-group-text">Currently</span> <div class="form-control d-flex h-auto"> <span class="text-break" style="flex-grow:1;min-width:0"> <a href=" /uploads/store/bernache_VfSjPGz.jpg <http://localhost:8080/uploads/store/bernache_VfSjPGz.jpg>"> store/bernache_VfSjPGz.jpg</a> </span> <span class="align-self-center ml-2"> <span class="form-check"> <input type="checkbox" name="form-0-image-clear" id=" form-0-image-clear_id" class="form-check-input" > <label class="form-check-label mb-0" for="form-0-image-clear_id">Clear</label> </span> </span> </div> </div> <div> <input type="file" name="form-0-image" class="form-control" accept=" image/*" id="id_form-0-image"> </div> </div> </div> <div class="mb-3"> <div id="div_id_form-0-visible" class="mb-3 "> <input type="checkbox" name="form-0-visible" class="checkboxinput form-check-input" id="id_form-0-visible" checked> <label for=" id_form-0-visible" class="form-check-label"> Visible </label> <small id="hint_id_form-0-visible" class="form-text text-muted">Select this field to activate this event on the home page</small> </div> </div> <div id="div_id_form-0-brief" class="mb-3"> <label for="id_form-0-brief" class=" form-label"> Brief </label> <input type="text" name="form-0-brief" value="BEACH PARTY" maxlength="200" class="textinput textInput form-control" id="id_form-0-brief "> <small id="hint_id_form-0-brief" class="form-text text-muted">A brief description of the event</small> </div> <div id="div_id_form-0-details" class="mb-3"> <label for="id_form-0-details" class="form-label"> Details </label> <textarea name="form-0-details" cols="40" rows="10" class="textarea form-control" id="id_form-0-details"> Dress code - All White. To a new year</textarea> <small id=" hint_id_form-0-details" class="form-text text-muted">Separate each line with a fullstop and leave the last line empty</small> </div> <div id=" div_id_form-0-venue" class="mb-3"> <label for="id_form-0-venue" class=" form-label"> Venue </label> <input type="text" name="form-0-venue" value="Port louis" maxlength ="200" class="textinput textInput form-control" id="id_form-0-venue"> </div> <div id="div_id_form-0-date" class="mb-3"> <label for="id_form-0-date" class="form-label requiredField"> Date<span class="asteriskField">*</span> </label> <input type="text" name=" form-0-date" value="2023-01-01" class="dateinput form-control" id=" id_form-0-date"><input type="hidden" name="initial-form-0-date" value=" 2023-01-01" id="initial-form-0-id_form-0-date"> </div> <div id=" div_id_form-0-time" class="mb-3"> <label for="id_form-0-time" class=" form-label"> Time </label> <input type="text" name="form-0-time" value="2100H" maxlength="20" class="textinput textInput form-control" id="id_form-0-time"> </div> <div id ="div_id_form-0-promoters" class="mb-3"> <label for="id_form-0-promoters" class="form-label"> Promoters </label> <input type="text" name="form-0-promoters" value="Live & Direk Entertainment" maxlength="200" class="textinput textInput form-control" id=" id_form-0-promoters"> </div> <div id="div_id_form-0-advance_ticket" class=" mb-3"> <label for="id_form-0-advance_ticket" class="form-label requiredField "> Advance ticket<span class="asteriskField">*</span> </label> <input type=" number" name="form-0-advance_ticket" value="0" min="0" class="numberinput form-control" id="id_form-0-advance_ticket"> </div> <div id=" div_id_form-0-gate_ticket" class="mb-3"> <label for="id_form-0-gate_ticket" class="form-label requiredField"> Gate ticket<span class="asteriskField">*</span> </label> <input type="number" name="form-0-gate_ticket" value="5000" min="0" class="numberinput form-control" id="id_form-0-gate_ticket"> </div> <div id=" div_id_form-0-regular_ticket" class="mb-3"> <label for=" id_form-0-regular_ticket" class="form-label requiredField"> Regular ticket<span class="asteriskField">*</span> </label> <input type=" number" name="form-0-regular_ticket" value="0" min="0" class="numberinput form-control" id="id_form-0-regular_ticket"> </div> <div id=" div_id_form-0-vip_ticket" class="mb-3"> <label for="id_form-0-vip_ticket" class="form-label requiredField"> Vip ticket<span class="asteriskField">*</span> </label> <input type="number" name="form-0-vip_ticket" value="10000" min="0" class="numberinput form-control" id="id_form-0-vip_ticket"> </div> <div id=" div_id_form-0-under18_ticket" class="mb-3"> <label for=" id_form-0-under18_ticket" class="form-label requiredField"> Under18 ticket<span class="asteriskField">*</span> </label> <input type=" number" name="form-0-under18_ticket" value="0" min="0" class="numberinput form-control" id="id_form-0-under18_ticket"> </div> <input type="hidden" name="form-0-id" value="7" id="id_form-0-id"> </div> <div class="multiField"> <div id="div_id_form-1-name" class="mb-3"> <label for="id_form-1-name" class ="form-label"> Name </label> <input type="text" name="form-1-name" maxlength="1000" class="textinput textInput form-control" id="id_form-1-name"> <small id="hint_id_form-1-name" class="form-text text-muted">Full name of the event</small> </div> <div id=" div_id_form-1-image" class="mb-3"> <label for="id_form-1-image" class=" form-label"> Image </label> <div class=" mb-2"> <div> <input type="file" name="form-1-image" class="form-control" accept="image/*" id="id_form-1-image"> </div> </div> </div> <div class="mb-3"> <div id="div_id_form-1-visible" class="mb-3 "> <input type="checkbox" name="form-1-visible" class="checkboxinput form-check-input" id="id_form-1-visible"> <label for="id_form-1-visible" class="form-check-label"> Visible </label> <small id="hint_id_form-1-visible" class="form-text text-muted">Select this field to activate this event on the home page</small> </div> </div> <div id="div_id_form-1-brief" class="mb-3"> <label for="id_form-1-brief" class=" form-label"> Brief </label> <input type="text" name="form-1-brief" maxlength="200" class="textinput textInput form-control" id="id_form-1-brief"> <small id=" hint_id_form-1-brief" class="form-text text-muted">A brief description of the event</small> </div> <div id="div_id_form-1-details" class="mb-3"> <label for="id_form-1-details" class="form-label"> Details </label> <textarea name="form-1-details" cols="40" rows="10" class="textarea form-control" id="id_form-1-details"> </textarea> <small id="hint_id_form-1-details" class="form-text text-muted">Separate each line with a fullstop and leave the last line empty</small> </div> <div id="div_id_form-1-venue" class="mb-3"> <label for="id_form-1-venue" class=" form-label"> Venue </label> <input type="text" name="form-1-venue" maxlength="200" class="textinput textInput form-control" id="id_form-1-venue"> </div> <div id=" div_id_form-1-date" class="mb-3"> <label for="id_form-1-date" class="form-label requiredField"> Date<span class="asteriskField">*</span> </label> <input type="text" name=" form-1-date" value="2023-01-22" class="dateinput form-control" id=" id_form-1-date"><input type="hidden" name="initial-form-1-date" value="2023-01-22 19:26:45+00:00" id="initial-form-1-id_form-1-date"> </div> <div id=" div_id_form-1-time" class="mb-3"> <label for="id_form-1-time" class=" form-label"> Time </label> <input type="text" name="form-1-time" maxlength="20" class="textinput textInput form-control" id="id_form-1-time"> </div> <div id=" div_id_form-1-promoters" class="mb-3"> <label for="id_form-1-promoters" class="form-label"> Promoters </label> <input type="text" name="form-1-promoters" maxlength="200" class="textinput textInput form-control" id="id_form-1-promoters"> </div> <div id=" div_id_form-1-advance_ticket" class="mb-3"> <label for=" id_form-1-advance_ticket" class="form-label requiredField"> Advance ticket<span class="asteriskField">*</span> </label> <input type=" number" name="form-1-advance_ticket" value="0" min="0" class="numberinput form-control" id="id_form-1-advance_ticket"> </div> <div id=" div_id_form-1-gate_ticket" class="mb-3"> <label for="id_form-1-gate_ticket" class="form-label requiredField"> Gate ticket<span class="asteriskField">*</span> </label> <input type="number" name="form-1-gate_ticket" value="0" min="0" class="numberinput form-control" id="id_form-1-gate_ticket"> </div> <div id="div_id_form-1-regular_ticket" class="mb-3"> <label for="id_form-1-regular_ticket" class="form-label requiredField"> Regular ticket<span class="asteriskField">*</span> </label> <input type=" number" name="form-1-regular_ticket" value="0" min="0" class="numberinput form-control" id="id_form-1-regular_ticket"> </div> <div id=" div_id_form-1-vip_ticket" class="mb-3"> <label for="id_form-1-vip_ticket" class="form-label requiredField"> Vip ticket<span class="asteriskField">*</span> </label> <input type="number" name="form-1-vip_ticket" value="0" min="0" class="numberinput form-control" id="id_form-1-vip_ticket"> </div> <div id="div_id_form-1-under18_ticket" class="mb-3"> <label for="id_form-1-under18_ticket" class="form-label requiredField"> Under18 ticket<span class="asteriskField">*</span> </label> <input type=" number" name="form-1-under18_ticket" value="0" min="0" class="numberinput form-control" id="id_form-1-under18_ticket"> </div> <input type="hidden" name="form-1-id" id="id_form-1-id"> </div> Regards, -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/63935db7-b4d5-4598-912e-bfa262b4f199n%40googlegroups.com.