DjangoRestFramework - 如何将不同序列化程序的结果合并为一个
DjangoRestFramework - How to get result from different serializers into one
我有4-5个模型如下:-
class Product(models.Model):
title = models.CharField(max_length=255)
short_description = models.TextField(blank=True,null=True)
video_link = models.URLField(blank=True,null=True)
price = models.IntegerField(db_index=True,blank=True,null=True)
user = models.ForeignKey(User)
@staticmethod
def get_product_details(product,category=None,image=None,comment=None,rating=None):
product_detail = {
'id' : product.id,
'title' : product.title,
'short_description' : product.short_description,
'video_link' : product.video_link,
'price' : product.price,
'user' : product.user.first_name + ' ' + product.user.last_name,
}
if category:
product_detail.update({'categories': ProductCategory.get_product_categories(product)})
if comment:
product_detail.update({'comments' : ProductComments.get_product_comments(product)})
if image:
product_detail.update({'images': ProductImages.get_product_images(product)})
if rating:
product_detail.update({'rating': ProductRating.return_data(product)})
return product_detail
class ProductCategory(models.Model):
subcategory = models.CharField(max_length=255)
product = models.ForeignKey(Product)
date = models.DateTimeField(auto_now_add=True)
@staticmethod
def get_product_categories(product):
return ProductCategory.objects.filter(product=product).values_list('subcategory',flat=True)
同样,我有多个用 Product
作为 FK 引用的模型(例如 ProductImages
、ProductComments
等),我需要从中获取数据然后将其集中显示在 Product
模型下使用 get_product_details
获取特定产品的数据。
因此,如果我浏览到 localhost:8080/product/<product_id>
,它将调用 get_product_details
,后者又会调用模型中的其他方法来收集 <product_id>
的信息引用。
是否可以在 Django-Rest-Framework 中创建一个序列化程序,它将从引用特定产品对象的其他序列化程序中获取数据??
以下是我的序列化程序。
class ProductSerializer(serializers.ModelSerializer):
class Meta:
model = Product
class ProductCategorySerializer(serializers.ModelSerializer):
class Meta:
model = ProductCategory
我期待的是这样的:-
class ProductSerializer(serializers.ModelSerializer):
class Meta:
model = Product
include_data_models = (ProductCategory,ProductImages,)
我的预期输出应为以下格式:-
[
{
"price": 1234,
"categories": [
"Tech"
],
"id": 1,
"title": "1",
"user": "user full name from User's model",
"video_link": "",
"short_description": ""
}
]
假设您使用的是 Django REST Framework v3.0+,我建议您查看 models.ForeignKey
字段中的 Django REST Framework Documentation on Serializer Relations. In addition, you will want to utilize ForeignKey.related_name。
型号
class Product(models.Model):
title = models.CharField(max_length=255)
short_description = models.TextField(blank=True,null=True)
video_link = models.URLField(blank=True,null=True)
price = models.IntegerField(db_index=True,blank=True,null=True)
user = models.ForeignKey(
to=User,
related_name="products",
)
...
class ProductCategory(models.Model):
subcategory = models.CharField(max_length=255)
product = models.ForeignKey(
to=Product,
related_name="categories",
)
date = models.DateTimeField(auto_now_add=True)
...
def __unicode__(self):
return self.subcategory # Tech, Books, etc.
序列化器
class ProductCategorySerializer(serializers.ModelSerializer):
class Meta:
model = ProductCategory
class ProductSerializer(serializers.ModelSerializer):
# The related_name="categories" on the ProductCategory.product
# will allow us to retrieve categories, and convert it to
# the __unicode__ equivalent.
# http://www.django-rest-framework.org/api-guide/relations/#stringrelatedfield
categories = serializers.StringRelatedField(many=True)
class Meta:
model = Product
我有4-5个模型如下:-
class Product(models.Model):
title = models.CharField(max_length=255)
short_description = models.TextField(blank=True,null=True)
video_link = models.URLField(blank=True,null=True)
price = models.IntegerField(db_index=True,blank=True,null=True)
user = models.ForeignKey(User)
@staticmethod
def get_product_details(product,category=None,image=None,comment=None,rating=None):
product_detail = {
'id' : product.id,
'title' : product.title,
'short_description' : product.short_description,
'video_link' : product.video_link,
'price' : product.price,
'user' : product.user.first_name + ' ' + product.user.last_name,
}
if category:
product_detail.update({'categories': ProductCategory.get_product_categories(product)})
if comment:
product_detail.update({'comments' : ProductComments.get_product_comments(product)})
if image:
product_detail.update({'images': ProductImages.get_product_images(product)})
if rating:
product_detail.update({'rating': ProductRating.return_data(product)})
return product_detail
class ProductCategory(models.Model):
subcategory = models.CharField(max_length=255)
product = models.ForeignKey(Product)
date = models.DateTimeField(auto_now_add=True)
@staticmethod
def get_product_categories(product):
return ProductCategory.objects.filter(product=product).values_list('subcategory',flat=True)
同样,我有多个用 Product
作为 FK 引用的模型(例如 ProductImages
、ProductComments
等),我需要从中获取数据然后将其集中显示在 Product
模型下使用 get_product_details
获取特定产品的数据。
因此,如果我浏览到 localhost:8080/product/<product_id>
,它将调用 get_product_details
,后者又会调用模型中的其他方法来收集 <product_id>
的信息引用。
是否可以在 Django-Rest-Framework 中创建一个序列化程序,它将从引用特定产品对象的其他序列化程序中获取数据??
以下是我的序列化程序。
class ProductSerializer(serializers.ModelSerializer):
class Meta:
model = Product
class ProductCategorySerializer(serializers.ModelSerializer):
class Meta:
model = ProductCategory
我期待的是这样的:-
class ProductSerializer(serializers.ModelSerializer):
class Meta:
model = Product
include_data_models = (ProductCategory,ProductImages,)
我的预期输出应为以下格式:-
[
{
"price": 1234,
"categories": [
"Tech"
],
"id": 1,
"title": "1",
"user": "user full name from User's model",
"video_link": "",
"short_description": ""
}
]
假设您使用的是 Django REST Framework v3.0+,我建议您查看 models.ForeignKey
字段中的 Django REST Framework Documentation on Serializer Relations. In addition, you will want to utilize ForeignKey.related_name。
型号
class Product(models.Model):
title = models.CharField(max_length=255)
short_description = models.TextField(blank=True,null=True)
video_link = models.URLField(blank=True,null=True)
price = models.IntegerField(db_index=True,blank=True,null=True)
user = models.ForeignKey(
to=User,
related_name="products",
)
...
class ProductCategory(models.Model):
subcategory = models.CharField(max_length=255)
product = models.ForeignKey(
to=Product,
related_name="categories",
)
date = models.DateTimeField(auto_now_add=True)
...
def __unicode__(self):
return self.subcategory # Tech, Books, etc.
序列化器
class ProductCategorySerializer(serializers.ModelSerializer):
class Meta:
model = ProductCategory
class ProductSerializer(serializers.ModelSerializer):
# The related_name="categories" on the ProductCategory.product
# will allow us to retrieve categories, and convert it to
# the __unicode__ equivalent.
# http://www.django-rest-framework.org/api-guide/relations/#stringrelatedfield
categories = serializers.StringRelatedField(many=True)
class Meta:
model = Product