我需要计算一个对象拥有的子节点数,并通过对象序列化器在我的API中返回该值。我还需要计算这些子对象的子集。
我有一个带有孩子Asignees的Task对象。在我的API中,当我查询任务时,我希望返回以下数据集:
[
{ label: "Cross the bridge",
count_assigned: 5,
count_completed: 3 },
{ label: "Build a fire",
count_assigned: 5,
count_completed: 2 }
]
我该怎么做?我找到了.annotate()方法,但该结果在序列化程序类中不可用。
models.py
class Task(models.Model):
label = models.CharField(max_length=255,null=False,blank=False)
class Assignee(models.model):
task = models.ForeignKey(Task, related_name='assignees', on_delete=models.CASCADE, blank=True, null=True)
person = models.ForeignKey(Person, on_delete=models.CASCADE, blank=True, null=True)
completed = models.DateTimeField(null=True,blank=True)
serializers.py
from rest_framework import serializers
from .models import Task, Assignee
from people.serializers import PersonSerializer
class AssigneeSerializer(serializers.ModelSerializer):
id = serializers.ReadOnlyField()
person = PersonSerializer(read_only=True)
class Meta:
model = Assignee
fields = ('id','task','person','completed')
read_only_fields = ['id']
class TaskSerializer(serializers.ModelSerializer):
id = serializers.ReadOnlyField()
class Meta:
model = Task
fields = ('id', 'label')
read_only_fields = ['id']
答案 0 :(得分:2)
建议的方式
SqlConnection conn = connectLocaldb.ConnectDataBase();
conn.Open();
string sql = "INSERT INTO MemberCommitBeforeCompiling(Username,ProjectName,Version,GroupName,CommitTime,Branch) VALUES ('" + push.user_name + "','" + push.project.name + "','" + push.after + "','" + groupname + "',getdate(),'" + push.@ref + "') ";
SqlCommand cmd = new SqlCommand(sql, conn);
int result = cmd.ExecuteNonQuery();
http://www.django-rest-framework.org/api-guide/fields/#serializermethodfield
答案 1 :(得分:0)
如果我理解你的逻辑,你可以尝试
在序列化程序中
class TaskSerializer(serializers.ModelSerializer):
count_assigned = serializers.IntegerField(read_only=True)
count_completed = serializers.IntegerField(read_only=True)
然后通过queryset:
from django.db.models import Count, Case, When, IntegerField
qs = Task.objects.annotate(
count_completed=Count(Case(
When(assignees__completed__isnull=False, then=1),
output_field=IntegerField(),
))
).annotate(count_assigned=Count('assignees'))
serializer = TaskSerializer(qs, many=True)
模型中的效率非常低:
from django.utils.functional import cached_property
class Task(models.Model):
@cached_property
def all_assignee(self):
return self.assignees.all()
def count_assigned(self):
return self.all_assignee.count()
def count_completed(self):
return self.all_assignee.filter(completed__isnull=False).count()