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>