DRF如何仅显示序列化嵌套对象的非空值
DRF How to display only not blank value of serializing nested object
我创建了序列化嵌套 be 使用序列化器作为文档
models.py
class Category(models.Model):
name = models.CharField("Name", "name", max_length=255)
iconname = models.CharField("Icon Name", "iconname", max_length=255)
budgetamount = models.DecimalField(
max_digits=19, decimal_places=2, default=0)
iconcolor = models.CharField(
"Icon Color", "iconcolor", default='4294951175', max_length=255)
def __str__(self):
return self.name
class DailyExpense(models.Model):
payee_item_desc = models.CharField(
"Payee Item Description", "payee_item_desc", max_length=255)
category = models.ForeignKey(
Category, related_name='dailyexpense_category', on_delete=models.CASCADE, blank=True, null=True)
amount = models.DecimalField(max_digits=19, decimal_places=2)
remarks = models.CharField(
"Remarks", "remarks", max_length=255, blank=True, null=True)
tran_date = models.DateTimeField()
isnotclear = models.BooleanField(default=False)
def __str__(self):
return self.payee_item_desc
serializers.py
class DailyExpenseSerializer(serializers.ModelSerializer):
class Meta:
model = DailyExpense
fields = "__all__"
class CategoryWithDailyExpenseSerializer(serializers.ModelSerializer):
dailyexpense_category = DailyExpenseSerializer(
source='filtered_dailyexpense_category', many=True, read_only=True)
class Meta:
model = Category
fields = ('id', 'name', 'iconname',
'budgetamount', 'iconcolor', 'dailyexpense_category')
views.py
class CategoryWithDailyExpenseViewSet(viewsets.ModelViewSet):
def get_queryset(self):
fromDate = parse_datetime(self.request.query_params.get(
'fromDate') + ' ' + '00:00:00').strftime('%Y-%m-%d %H:%M:%S')
toDate = parse_datetime(self.request.query_params.get(
'toDate') + ' ' + '00:00:00').strftime('%Y-%m-%d %H:%M:%S')
queryset = Category.objects.prefetch_related(
Prefetch('dailyexpense_category', queryset=DailyExpense.objects.filter(
tran_date__range=[fromDate, toDate]).order_by('tran_date'), to_attr='filtered_dailyexpense_category')
)
return queryset
# queryset = Category.objects.all().order_by('name')
serializer_class = CategoryWithDailyExpenseSerializer
filter_class = CategoryFilter
我得到的结果如下
[
{
"id": 1,
"name": "Auto",
"iconname": "taxi_alert_outlined",
"budgetamount": "0.00",
"iconcolor": "4294951175",
"dailyexpense_category": []
},
{
"id": 2,
"name": "Foods:Breakfast",
"iconname": "emoji_food_beverage",
"budgetamount": "0.00",
"iconcolor": "4294951175",
"dailyexpense_category": [
{
"id": 24574,
"payee_item_desc": "เซเว่น",
"amount": "-100.00",
"remarks": "เฟิส",
"tran_date": "2022-04-01T00:00:00Z",
"isnotclear": false,
"category": 2
}
]
}
]
但我只想显示非空白嵌套对象,如下所示
[
{
"id": 2,
"name": "Foods:Breakfast",
"iconname": "emoji_food_beverage",
"budgetamount": "0.00",
"iconcolor": "4294951175",
"dailyexpense_category": [
{
"id": 24574,
"payee_item_desc": "เซเว่น",
"amount": "-100.00",
"remarks": "เฟิส",
"tran_date": "2022-04-01T00:00:00Z",
"isnotclear": false,
"category": 2
}
]
}
]
我尝试创建自定义字段,例如对嵌套对象求和或对嵌套对象计数
但我想要一些更容易过滤的东西,而不是空白反对。
我认为您需要添加如下过滤器。
queryset = Category.objects.prefetch_related(
Prefetch('dailyexpense_category',
queryset=DailyExpense.objects.filter(tran_date__range=[fromDate, toDate]).order_by('tran_date'))
).annotate(num_daily=Count('dailyexpense_category', filter = Q('dailyexpense_category__tran_date__range' =[fromDate, toDate]))).filter(num_daily__gt=0)
希望对您有所帮助。
我创建了序列化嵌套 be 使用序列化器作为文档
models.py
class Category(models.Model):
name = models.CharField("Name", "name", max_length=255)
iconname = models.CharField("Icon Name", "iconname", max_length=255)
budgetamount = models.DecimalField(
max_digits=19, decimal_places=2, default=0)
iconcolor = models.CharField(
"Icon Color", "iconcolor", default='4294951175', max_length=255)
def __str__(self):
return self.name
class DailyExpense(models.Model):
payee_item_desc = models.CharField(
"Payee Item Description", "payee_item_desc", max_length=255)
category = models.ForeignKey(
Category, related_name='dailyexpense_category', on_delete=models.CASCADE, blank=True, null=True)
amount = models.DecimalField(max_digits=19, decimal_places=2)
remarks = models.CharField(
"Remarks", "remarks", max_length=255, blank=True, null=True)
tran_date = models.DateTimeField()
isnotclear = models.BooleanField(default=False)
def __str__(self):
return self.payee_item_desc
serializers.py
class DailyExpenseSerializer(serializers.ModelSerializer):
class Meta:
model = DailyExpense
fields = "__all__"
class CategoryWithDailyExpenseSerializer(serializers.ModelSerializer):
dailyexpense_category = DailyExpenseSerializer(
source='filtered_dailyexpense_category', many=True, read_only=True)
class Meta:
model = Category
fields = ('id', 'name', 'iconname',
'budgetamount', 'iconcolor', 'dailyexpense_category')
views.py
class CategoryWithDailyExpenseViewSet(viewsets.ModelViewSet):
def get_queryset(self):
fromDate = parse_datetime(self.request.query_params.get(
'fromDate') + ' ' + '00:00:00').strftime('%Y-%m-%d %H:%M:%S')
toDate = parse_datetime(self.request.query_params.get(
'toDate') + ' ' + '00:00:00').strftime('%Y-%m-%d %H:%M:%S')
queryset = Category.objects.prefetch_related(
Prefetch('dailyexpense_category', queryset=DailyExpense.objects.filter(
tran_date__range=[fromDate, toDate]).order_by('tran_date'), to_attr='filtered_dailyexpense_category')
)
return queryset
# queryset = Category.objects.all().order_by('name')
serializer_class = CategoryWithDailyExpenseSerializer
filter_class = CategoryFilter
我得到的结果如下
[
{
"id": 1,
"name": "Auto",
"iconname": "taxi_alert_outlined",
"budgetamount": "0.00",
"iconcolor": "4294951175",
"dailyexpense_category": []
},
{
"id": 2,
"name": "Foods:Breakfast",
"iconname": "emoji_food_beverage",
"budgetamount": "0.00",
"iconcolor": "4294951175",
"dailyexpense_category": [
{
"id": 24574,
"payee_item_desc": "เซเว่น",
"amount": "-100.00",
"remarks": "เฟิส",
"tran_date": "2022-04-01T00:00:00Z",
"isnotclear": false,
"category": 2
}
]
}
]
但我只想显示非空白嵌套对象,如下所示
[
{
"id": 2,
"name": "Foods:Breakfast",
"iconname": "emoji_food_beverage",
"budgetamount": "0.00",
"iconcolor": "4294951175",
"dailyexpense_category": [
{
"id": 24574,
"payee_item_desc": "เซเว่น",
"amount": "-100.00",
"remarks": "เฟิส",
"tran_date": "2022-04-01T00:00:00Z",
"isnotclear": false,
"category": 2
}
]
}
]
我尝试创建自定义字段,例如对嵌套对象求和或对嵌套对象计数 但我想要一些更容易过滤的东西,而不是空白反对。
我认为您需要添加如下过滤器。
queryset = Category.objects.prefetch_related(
Prefetch('dailyexpense_category',
queryset=DailyExpense.objects.filter(tran_date__range=[fromDate, toDate]).order_by('tran_date'))
).annotate(num_daily=Count('dailyexpense_category', filter = Q('dailyexpense_category__tran_date__range' =[fromDate, toDate]))).filter(num_daily__gt=0)
希望对您有所帮助。