Django
2
Вклад в тег
class Basket(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='basket')
class BasketProducts(models.Model):
basket= models.ForeignKey(Basket, on_delete=models.CASCADE, related_name='basket_products')
products = models.ManyToManyField(Products, on_delete=models.CASCADE, related_name="basket_products")
class Products(models.Model):
name = models.CharField(max_length=255)
'''''
class ProductsSerializer(serializers.ModelSerializer):
class Meta:
model = Products
fields = ("__all__")
class BsketProductsSerializer(serializers.HyperlinkedModelSerializer):
products = ProductsSerializer(many=True)
class Meta:
model = BasketProducts
fields = ('basket','products')
class BasketSerializer(serializers.HyperlinkedModelSerializer):
basket_products = BsketProductsSerializer(many=True)
class Meta:
model = Basket
fields = ('user' , 'basket_products ')
class UserSerializer(serializers.HyperlinkedModelSerializer):
basket = BasketSerializer(many=True)
class Meta:
model = User
fields = ('id', 'phone','email' ,'basket')