Registering Django objects with child objects

I have two tables named product and variant. I am building an offer creation module. While creating the offer, I want to pull the products from the product and variant table and save them to the offer product table, but I am having problems with the checkboxes.

Can you help me @KenWhitesell
model.py

from django.db import models
from firma.models import company

class variant(models.Model):
    name = models.CharField('Varyant Adı',max_length=200)
    price = models.IntegerField('Varyant Fiyat')    
    stok = models.IntegerField('Varyant Stok')    
    company = models.ForeignKey(company,on_delete=models.CASCADE)
    image = models.FileField(upload_to="variant",default="")
    def __str__(self):
        return f"{self.name}"
class product(models.Model):
    name = models.CharField('Ürün Adı',max_length=200)
    price = models.IntegerField('Ürün Fiyat')
    stok = models.IntegerField('Ürün Stok')
    variant = models.ManyToManyField(variant,blank=True)
    company = models.ForeignKey(company,on_delete=models.CASCADE)
    image = models.FileField(upload_to="product",default="")
    def __str__(self):
        return f"{self.name}"

model.py

class offerProductInfo(models.Model):
    offerProductName = models.CharField('Ürün Adı',max_length=200)
    offerProductPrice = models.IntegerField('Ürün Fiyat')
    offerProductStok = models.IntegerField('Ürün Stok')
    offerProductVariant = models.ManyToManyField(offerVariantInfo,blank=True)
    offerProductCompany = models.ForeignKey(company,on_delete=models.CASCADE)
    offerProductImage = models.ImageField(upload_to="product")
    def __str__(self):
        return f"{self.offerProductName}" 

view.py

def teklifsave(request):
    if not request.user.is_authenticated:
        return redirect('login')
    if request.method == 'POST':
        offerVariantId = request.POST.getlist('offerVariantId')
        offerProduct = request.POST.getlist("offerProducts")
        count = -1
        for fproduct in offerProduct:
            print(fproduct)
            count += 1
            h = variant.objects.get(id=offerVariantId[count])
            c = h.product_set.all()
            print(c)
            s = offerVariantInfo(
                offerVariantName = h.name,
                offerVariantPrice = h.price,
                offerVariantStok = h.stok,
                offerVariantCompany = h.company
            )
            s.save()
            z = product.objects.get(id=fproduct)
            c = offerProductInfo(
                offerProductName = z.name,
                offerProductPrice = z.price,
                offerProductStok = z.stok,
                offerProductCompany = z.company,
                offerProductImage = z.image
            )
            c.save()
            m.offerProduct.add(c)
            c.offerProductVariant.add(s)
        return redirect('offer')
    return redirect('offer')

template (I have it in my elements and it does the post processing, I didn’t add it because there is too much code in the top and bottom tar)

<div id="kt_modal_add_customer_billing_info" class="collapse show">				
															{% for product in products %}
																<!--begin::Input group-->
																<div id="offerProductsVariants">

																
																<div class="fv-row mb-7">
																	<!--begin::Option-->
																		<label class="btn btn-outline btn-outline-dashed btn-outline-default p-7 d-flex align-items-center mb-5" for="kt_radio_buttons_2_option_1">
																			<!--begin::Svg Icon | path: icons/duotune/coding/cod001.svg-->
																			<span class="svg-icon svg-icon-4x me-4">
																				<img style="width:150px;" src="{{ product.image.url }}" />
																			</span>
																			<!--end::Svg Icon-->
																			<span class="d-block fw-bold text-start">
																				<span class="text-dark fw-bolder d-block fs-3">{{ product.name }}</span>
																				<span class="text-muted fw-bold fs-6">
																					₺{{ product.price }}
																				</span>
																			</span>
																			<div class="form-check form-check-custom form-check-solid" id="product-check" style="position:absolute; right:60;">
																				<div id="{{ offerForm.offerProduct.auto_id }}">
																					<div>
																						<label for='id_offerProduct'>
																							<input class="form-check-input offerProductsCheckbox" type="checkbox" name='offerProducts' value="{{ product.id }}" id="id_offerProduct_{{ product.id }}"/>
																						</label>
																					</div>
																				</div>
																			</div>
																			
																		</label>
																		<!--end::Option-->
																		<!--begin::Accordion-->

																		<div class="accordion" id="kt_accordion_1">
																			<div class="accordion-item">
																				<h2 class="accordion-header" id="kt_accordion_1_header_{{ product.id }}">
																					<button class="accordion-button fs-4 fw-bold collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#kt_accordion_1_body_{{ product.id }}" aria-expanded="true" aria-controls="kt_accordion_1_body_{{ product.id }}">
																						{{ product.name }}'in Alt Ürünleri
																					</button>
																				</h2>
																				<div id="kt_accordion_1_body_{{ product.id }}" class="accordion-collapse collapse" aria-labelledby="kt_accordion_1_header_{{ product.id }}" data-bs-parent="#kt_accordion_1">
																					<div class="accordion-body">
																						{% for variant in product.variant.all %}
																							<div class="variant">
																								<b>{{ variant.name }} </b>- ₺<b>{{ variant.price }}</b><br />
																									<div class="form-check form-check-custom form-check-solid" id="variant-check" style="position:absolute; right:60;">
																									<div id="{{ offerForm2.offerVariant.auto_id }}">
																										<div>
																											<label for='id_offerVariant'>
																												<input class="form-check-input offerVariantsCheckbox" type="checkbox" name='offerVariantId' value="{{ product.id }},{{ variant.id }}" id="id_offerVariant_{{ variant.id }}"/>
																											</label>
																										</div>
																									</div>
																								</div>
																							</div>
																						{% endfor %}
																					</div>
																				</div>
																			</div>
																		</div>

																		<!--end::Accordion-->
																</div>
																</div>
																<!--end::Input group-->
															{% endfor %}
															
														</div>

I’m sorry, I’m not sure I’m following what you’re asking for here.

You’re showing references to tables that you’re not showing the models (e.g. offerProductInfo has a field named offerProductVariant which is a ManyToManyField with an offerVariantInfo model - which isn’t shown here anywhere.)

It’s also difficult to read your code since it doesn’t follow any of the standard Python / Django code conventions.

I’m also extremely confused by you having a ManyToMany relationship between product and variant. On the surface, this doesn’t make sense for this type of application. (If I’m interpreting your intent correctly, I could see variant having a ForeignKey to product, but I’m not sure that’s the real need for your application.)

My first reaction to all this is that you should at a minimum ensure that your models are a proper relational model for your business requirements.

Then I would suggest adopting the Django coding conventions, but for the moment that’s not critical.

Finally, can you be more specific about:

What exactly is the problem you are encountering? (What is happening that you’re not expecting to happen, or what is not happening that you are expecting to see?)