Reputation: 279
I have two models. In first you can create name of product and write a price. Second model is a cart which contains these products.
Now I want create a form where user can propose cost of products in cart which I will make. For example I created cart1 which contains product1 and product2. I want give possibility to edit this price in cart form.
How can I do it? Here is my code:
models.py
class Product(models.Model):
name = models.CharField(max_length=100)
price = models.IntegerField(default='0')
def __unicode__(self):
return u"{}({})".format(self.name, self.price)
class Cart(models.Model):
product = models.ManyToManyField(Product)
name = models.CharField(max_length=50)
def __unicode__(self):
return self.name
forms.py
class CartForm(forms.ModelForm):
product = forms.ModelMultipleChoiceField(queryset = Product.objects.all(), widget=forms.CheckboxSelectMultiple(),required=True)
name = forms.CharField(max_length=45, label='nazwa')
price = forms.IntegerField(label='price')
class Meta:
model = Cart
fields = ('product', 'name', 'price')
Here is photo what I have and what I want (now i have one price - to all products, I want one price for one product):
Second problem: now I have checkbox, how can I do that there is no checkbox or list but everything must be automatically choose (user must choose all products from this cart).
EDIT: Now i have this:
class CartForm(forms.ModelForm):
product = forms.ModelMultipleChoiceField(queryset = Product.objects.all(), widget=forms.CheckboxSelectMultiple(),required=True)
name = forms.CharField(max_length=45, label='nazwa')
price = forms.IntegerField(label='price')
class Meta:
model = Cart
fields = ('product', 'name', 'price')
IngredientFormSet = inlineformset_factory(Cart, Product)
views.py:
def cart_new(request):
if request.method == "POST":
form = CartForm(request.POST)
if form.is_valid():
cart = form.save(commit=False)
cart.save()
form.save_m2m()
ingredient_formset = IngredientFormSet(request.POST)
if ingredient_formset.is_valid():
ingredient = formset.save(commit=False)
ingredient_formset.save()
return redirect('shop.views.cart_detail', pk=cart.pk)
else:
form = CartForm()
return render(request, 'shop/cart_edit.html', {'form': form})
Upvotes: 1
Views: 882
Reputation: 470
You could use InlineFormSetView provided by Django Extra Views. Here is an example copied from the doc:
from extra_views import InlineFormSetView
class EditProductReviewsView(InlineFormSetView):
model = Product
inline_model = Review
...
Upvotes: 1
Reputation: 20539
First of all, you need an formset for your sub-model (product). Then in your view you should access both: form for main model and formset for submodel and save it. An example of forms:
from django import forms
from django.forms.models import inlineformset_factory
# ... import here your models
class CartForm(forms.ModelForm):
class Meta:
model = Cart
fields = ('product', 'name', 'price')
IngredientFormSet = inlineformset_factory(Cart, Product)
And then use it in your view.
Upvotes: 0