How can in create inline formset which share the same foreign key using function base views. I don’t want to keep selecting product title(which is the FK to other forms) because am using two forms with linked to one Foreign key#
i want to implement this https://www.letscodemore.com/blog/django-inline-formset-factory-with-examples/ in function base views
I have these 3 models
#product model
class Product(models.Model):
title = models.CharField(max_length=150)
short_description = models.TextField(max_length=100)
def __str__(self):
return self.title
*Image model*
class Image(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE, null=True)
image = models.ImageField(blank=True, upload_to='images')
def __str__(self):
return self.product.title
*variant model*
class Variant(models.Model):
product = models.ForeignKey( Product, on_delete=models.CASCADE)
size = models.CharField(max_length=100)
quantity = models.PositiveIntegerField(default=1)
price = models.DecimalField(max_digits=12, decimal_places=2)
def __str__(self):
return self.product.title
Forms
from django import forms from
django.forms import inlineformset_factory
from .models import ( Product, Image, Variant)
class ProductForm(forms.ModelForm):
class Meta:
model = Product
fields = '__all__'
widgets = {
'title': forms.TextInput(
attrs={ 'class': 'form-control'} ), 'short_description': forms.TextInput(
attrs={'class': 'form-control'}),
}
class ImageForm(forms.ModelForm):
class Meta:
model = Image
fields = '__all__'
class VariantForm(forms.ModelForm):
class Meta:
model = Variant
fields = '__all__'
widgets = {
'size': forms.TextInput(attrs={'class': 'form-control'} ),
'quantity': forms.NumberInput(attrs={'class': 'form-control'}),
'price': forms.NumberInput(attrs={ 'class': 'form-control'}),
}
VariantFormSet = inlineformset_factory( Product, Variant, form=VariantForm, extra=1, can_delete=True, can_delete_extra=True )
ImageFormSet = inlineformset_factory( Product, Image, form=ImageForm,extra=1, can_delete=True, can_delete_extra=True )
Views
**Views**
from django.shortcuts import render, redirect
from django.contrib import messages
from .forms import (ProductForm, VariantFormSet, ImageFormSet)
from .models import (Image, Product, Variant)
#create product
def create_product(request):
if method.request == 'POST':
form = ProductForm(request.POST)
if form.is valid():
form.save()
else: form = ProductForm()
return redirect('product:products')
What i tried
How can in create inline formset which share the same foreign key using function base views. I don’t want to keep selecting product title(which is the FK to other forms) because am using two forms with linked to one Foreign key#
What i tried
**Views**
from django.shortcuts import render, redirect
from django.contrib import messages
from .forms import (ProductForm, VariantFormSet, ImageFormSet)
from .models import (Image, Product, Variant)
#create product
def create_product(request):
if method.request == 'POST':
form = ProductForm(request.POST)
if form.is valid():
form.save()
else: form = ProductForm()
return redirect('product:products')
What i tried
#Attach Image and variant to the product
when submitting this form, the VariantForm should get FK(product title field) from the ImageForm FK which has already been selected in the django template
#Attach product image and variation to product
def add_image_and_variant(request):
if method.request == 'POST':
image_form = ImageForm(request.POST,request == 'FILES')
var_form = VariantForm(request.POST)
if image_form.is valid() and var_form():
image_instance =image_form.save()
var = var_form(commit = False)
var = var(image_instance.title)
var.save()
else:
image_form = ImageForm()
var_form = VariantForm()
return redirect('product:products')
return render(request,'product_var.html',{'image_form':image_form,:var_form})