У меня есть кортеж, и я хочу сохранить его в базе данных. как я могу сделать это с помощью django
вот изображение моей базы данных CATEGORY_CHOICES = (('M', 'Mobile'), ('L', 'Laptop'), ('TW', 'Верхняя одежда'), ('BW', 'Нижняя одежда'), ('W', 'Часы'), ('P', 'принтер'), ('F', 'вентилятор'), ('EB', 'наушники'), ('C', 'Камера'), ('O', 'Масло'), ('SH', 'Душ'), ('MU', 'Музели'), ('CL', 'Чистящее средство'), ('CA', 'Компьютер и аксессуары'))
models.py
class Product(models.Model):
title = models.CharField(max_length=200)
selling_price = models.FloatField()
discounted_price = models.FloatField()
description = models.TextField()
brand = models.CharField(max_length=100)
category = models.CharField(choices=CATEGORY_CHOICES, max_length=2)
product_image = models.ImageField(upload_to='productimg')
def __str__(self):
return str(self.id)
views.py
@login_required
def upload_details(request):
category = CATEGORY_CHOICES
dict_category = dict(category)
print("Category", type(category))
if request.method == "POST":
product_name = request.POST.get('product-title')
product_selling_price = request.POST.get('product-selling-price')
product_discounted_price = request.POST.get('product-discounted-price')
product_description = request.POST.get('product-description')
product_brand = request.POST.get('product-brand')
product_category = request.POST.get('product-category')
product_main_image = request.FILES['product-main-image']
print("Product Category", type(product_category))
save_product = Product(title=product_name, selling_price=product_selling_price,
discounted_price=product_discounted_price, description=product_description,
brand=product_brand.upper(), category=product_category, product_image=product_main_image)
save_product.save()
return render(request, 'app/uploaddetails.html', {'dict_category': dict_category})
views.py
class ProductView(View):
def get(self, request):
totalitem = 0
topwears = Product.objects.filter(category='TW')
bottomwears = Product.objects.filter(category='BW')
mobiles = Product.objects.filter(category='M')
laptops = Product.objects.filter(category='L')
watch = Product.objects.filter(category='W')
computer_accessories = Product.objects.filter(category='CA')
random_watch = list(watch)
watch_shuffle = random.sample(random_watch, 14)
random_mobile = list(mobiles)
mobile_shuffle = random.sample(random_mobile, 9)
mobile_shuffle_mob = random.sample(random_mobile, 4)
random_item = list(Product.objects.all())
item_shuffle = random.sample(random_item, 20)
if request.user.is_authenticated:
totalitem = len(Cart.objects.filter(user=request.user))
return render(request, 'app/home.html', {'topwears': topwears, 'bottomwears': bottomwears, 'mobiles': mobiles, 'laptops': laptops, 'watch': watch, 'computer_accessories': computer_accessories, 'watch_shuffle': watch_shuffle, 'mobile_shuffle': mobile_shuffle, 'mobile_shuffle_mob': mobile_shuffle_mob, 'item_shuffle': item_shuffle, 'totalitem': totalitem})