Home > Software design >  I can't get product vendor id in django
I can't get product vendor id in django

Time:05-26

I am working on an e-commerce project. But the product vendor id is not registered in the database. I've tried many ways. I would be glad if you help. seller_id always comes empty What are your suggestions?

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(),
        }

enter image description here

CodePudding user response:

if form.is_valid():
    product = form.save(commit=False)
    product.seller = <insert here some Shop model instance>
    product.save()

Note that product.seller has to be a Shop instance not an user like in your example.

  • Related