Django get related objects ManyToMany relationships

前端 未结 1 569
迷失自我
迷失自我 2021-02-06 13:11

i have two models:

class CartToys(models.Model):
    name = models.CharField(max_length=350)
    quantity = models.IntegerField()

class Cart(models.Model):
             


        
相关标签:
1条回答
  • 2021-02-06 13:40

    you would use...

    cart = Cart.objects.first()
    objects = cart.cart_item.all() # this line return all related objects for CartToys
    # and in reverse
    cart_toy = CartToys.objects.first()
    carts = cart_toy.cart_set.all() # this line return all related objects for Cart
    
    0 讨论(0)
提交回复
热议问题