Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I can't get product vendor id in django

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading