1👍
✅
In your UserProduct
class, the field category
is a ForeignKey to the Category
class. Try getting the Category
first from your request.POST
, then adding it:
def sell(request):
category = Category.objects.all()
context = {'category': category}
if request.POST:
product_name = request.POST.get('product_name')
up_category = request.POST.get('up_category')
keywords = request.POST.get('keywords')
descriptions = request.POST.get('descriptions')
main_image = request.POST.get('main_image')
price = request.POST.get('price')
detail = request.POST.get('detail')
image1 = request.POST.get('image1')
image2 = request.POST.get('image2')
image3 = request.POST.get('image3')
if product_name == '' or up_category == 'Kateqoriya seç' or keywords == '' or descriptions == '' or price == '' or price == str or detail == '':
messages.warning(request, 'Bütün xanaları doldurduğunuzdan əmin olun!')
else:
cat = Category.objects.get(pk=up_category) # CHANGED
newUserProduct = UserProduct(user=request.user,
name=product_name,
category=cat, # CHANGED
keywords=keywords,
descriptions=descriptions,
detail=detail,
main_image=main_image,
image1 = image1,
image2 = image2,
image3 =image3
)
newUserProduct.save()
messages.warning(request, 'Məhsulunuz satışa çıxdı.')
return render(request, 'forms/sell.html', context)
return render(request, 'forms/sell.html', context)
Source:stackexchange.com