Home > database >  I am trying to add many to many feild in django but getting error
I am trying to add many to many feild in django but getting error

Time:11-26

Models are-

class Product(models.Model):

    name = models.CharField(max_length=50)
    price = models.IntegerField(default=0)
    seller=models.ForeignKey(Seller,null=True,on_delete=models.CASCADE,)        
    category = models.ForeignKey(Category,null=True, on_delete=models.CASCADE,)
    description = models.CharField(max_length=200, default='' , null=True , blank=True)
    image = models.ImageField(upload_to='uploads/products/')
    rating=models.FloatField(null=True)
    people=models.IntegerField(default=0,null=True)
    customers=models.ManyToManyField(Customer,blank=True)

class Customer(models.Model):

    user_name = models.CharField(max_length=50)
    phone = models.IntegerField(default='8928')
    email = models.EmailField()
    password = models.CharField(max_length=500)
        

        qq=Product.objects.get(id=8) 
        print(qq)
        print(Customer.objects.all().first()) 
        qq.customers.add(Customer.objects.all().first())
        print("qq.customers is",qq.customers)
"""
Product object (8)
Customer object (1)
qq.customers is store.Customer.None"""

I want to know how we can add customers in product.customers which is showing none in this problem .Please help me to solve this

CodePudding user response:

As far as I understand, the solution to the exact problem you encountered is available in the link below.

If this is not the case, please specify so we can talk about it again.

https://www.youtube.com/watch?v=BeVKKNhsRR4&list=PLIUezwWmVtFXaHcJ63ZM6uOJdhMrnZFFk&index=9&ab_channel=YukselCELIK,PhD

CodePudding user response:

print(qq.customers.all())

It will show customers.

  • Related