我无法在 Django 中获取产品供应商 ID
I can't get product vendor id in django
我正在做一个 e-commerce 项目。但产品供应商 ID 未在数据库中注册。我试过很多方法。如果你能帮忙,我会很高兴。
seller_id 总是空的
你有什么建议?
models.py
class Product(models.Model):
name = models.CharField(verbose_name="Məhsulun adı", max_length=150)
description = models.TextField(
verbose_name="Məhsul haqda məlumat", null=True)
price = models.FloatField(verbose_name="Məhsulun Qiymət")
quantity = models.IntegerField(verbose_name="Məhsulun sayı",)
seller = models.ForeignKey(
Shop,
on_delete=models.CASCADE,
verbose_name="Məhsulun satıcısı",
null=True
)
categories = models.ManyToManyField(
Category, verbose_name="Məhsulun kateqoriyaları", related_name='categories')
tags = models.ManyToManyField(Tags, verbose_name="Məhsulun etiketləri")
discount = models.IntegerField(
null=True, blank=True, verbose_name="Endirim", default=0)
active = models.BooleanField(default=True, verbose_name="Mağazada göstər",)
created_at = models.DateTimeField(auto_now_add=True, null=True)
colors = models.ManyToManyField(Colors, verbose_name='Məshulun rəngləri')
views.py
if request.method == 'POST' and 'addProduct' in request.POST:
main_image = request.FILES['main_image']
form = AddProductForm(request.POST)
images = request.FILES.getlist('images')
if form.is_valid():
form.seller = request.user.id
print(request.user.id)
form = form.save(commit=False)
form.save()
ProductImages.objects.create(product=form, image=main_image)
ProductInfo.objects.create(product=form)
for image in images:
print(image)
ProductImages.objects.create(product=form, image=image)
return redirect('shop:shopProducts')
else:
print(form.errors)
forms.py
class AddProductForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(AddProductForm, self).__init__(*args, **kwargs)
self.fields['price'].widget.attrs.update(
{'placeholder': 'Qiymət'})
self.fields['name'].widget.attrs.update(
{'placeholder': 'Məhsulun Adı'})
for field in self.fields:
self.fields[field].widget.attrs.update({'class': 'form-control'})
self.fields['active'].widget.attrs.update(
{'class': ''})
class Meta:
model = models.Product
fields = ['name', 'description', 'price', 'colors', 'discount', 'quantity', 'active', 'categories', ]
widgets = {
'price': forms.TextInput(),
'off': forms.TextInput(),
}
if form.is_valid():
product = form.save(commit=False)
product.seller = <insert here some Shop model instance>
product.save()
请注意,product.seller 必须是一个 Shop
实例,而不是您示例中的用户。
我正在做一个 e-commerce 项目。但产品供应商 ID 未在数据库中注册。我试过很多方法。如果你能帮忙,我会很高兴。 seller_id 总是空的 你有什么建议?
models.py
class Product(models.Model):
name = models.CharField(verbose_name="Məhsulun adı", max_length=150)
description = models.TextField(
verbose_name="Məhsul haqda məlumat", null=True)
price = models.FloatField(verbose_name="Məhsulun Qiymət")
quantity = models.IntegerField(verbose_name="Məhsulun sayı",)
seller = models.ForeignKey(
Shop,
on_delete=models.CASCADE,
verbose_name="Məhsulun satıcısı",
null=True
)
categories = models.ManyToManyField(
Category, verbose_name="Məhsulun kateqoriyaları", related_name='categories')
tags = models.ManyToManyField(Tags, verbose_name="Məhsulun etiketləri")
discount = models.IntegerField(
null=True, blank=True, verbose_name="Endirim", default=0)
active = models.BooleanField(default=True, verbose_name="Mağazada göstər",)
created_at = models.DateTimeField(auto_now_add=True, null=True)
colors = models.ManyToManyField(Colors, verbose_name='Məshulun rəngləri')
views.py
if request.method == 'POST' and 'addProduct' in request.POST:
main_image = request.FILES['main_image']
form = AddProductForm(request.POST)
images = request.FILES.getlist('images')
if form.is_valid():
form.seller = request.user.id
print(request.user.id)
form = form.save(commit=False)
form.save()
ProductImages.objects.create(product=form, image=main_image)
ProductInfo.objects.create(product=form)
for image in images:
print(image)
ProductImages.objects.create(product=form, image=image)
return redirect('shop:shopProducts')
else:
print(form.errors)
forms.py
class AddProductForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(AddProductForm, self).__init__(*args, **kwargs)
self.fields['price'].widget.attrs.update(
{'placeholder': 'Qiymət'})
self.fields['name'].widget.attrs.update(
{'placeholder': 'Məhsulun Adı'})
for field in self.fields:
self.fields[field].widget.attrs.update({'class': 'form-control'})
self.fields['active'].widget.attrs.update(
{'class': ''})
class Meta:
model = models.Product
fields = ['name', 'description', 'price', 'colors', 'discount', 'quantity', 'active', 'categories', ]
widgets = {
'price': forms.TextInput(),
'off': forms.TextInput(),
}
if form.is_valid():
product = form.save(commit=False)
product.seller = <insert here some Shop model instance>
product.save()
请注意,product.seller 必须是一个 Shop
实例,而不是您示例中的用户。