这是我的模型。py
class Language(models.Model):
language_id = models.BigAutoField(primary_key=True)
language_name = models.CharField(max_length=255)
created_on = models.DateTimeField(auto_now=True)
latest_build_on = models.DateTimeField(auto_now_add=True)
latest_version = models.DecimalField(max_digits=5, decimal_places=2)
company = models.OneToOneField('Company',on_delete=models.CASCADE,related_name='language')
def __str__(self):
return self.language_name
class Frameworks(models.Model):
framework_id = models.BigAutoField(primary_key=True)
framework_name = models.CharField(max_length=255)
framework_logo = models.FileField()
created_on = models.DateTimeField(auto_now=True)
latest_build_on = models.DateTimeField(auto_now_add=True)
latest_version = models.DecimalField(max_digits=5, decimal_places=2)
language = models.ForeignKey('Language',on_delete=models.CASCADE,related_name='frameworks')
def __str__(self):
return self.framework_name
class Technologies(models.Model):
technology_id = models.BigAutoField(primary_key=True)
technology_name = models.CharField(max_length=255)
description = models.TextField()
language = models.ManyToManyField('Language',related_name='technology')
def __str__(self):
return self.technology_name
这是serializers.py
class GetLanguageSerializer(serializers.ModelSerializer):
technology = serializers.StringRelatedField(many=True)
frameworks = serializers.StringRelatedField(many=True)
class Meta:
model = Language
fields = ('language_name','created_on','latest_build_on','latest_version','company','technology','frameworks')
depth = 1
class LanguageSerializer(serializers.ModelSerializer):
class Meta:
model = Language
fields = ('language_name','created_on','latest_build_on','latest_version','company')
class GetFrameworksSerializer(serializers.ModelSerializer):
language = serializers.StringRelatedField()
class Meta:
model = Frameworks
fields = '__all__'
depth = 1
class FrameworksSerializer(serializers.ModelSerializer):
class Meta:
model = Frameworks
fields = '__all__'
这是我的观点。py
class LanguageView(viewsets.ModelViewSet):
queryset = Language.objects.all()
serializer_class = LanguageSerializer
filter_backends = [SearchFilter,OrderingFilter]
search_fields = ['language_name']
def get_serializer_class(self):
serializer_class = self.serializer_class
if self.request.method == 'GET':
serializer_class = GetLanguageSerializer
return serializer_class
class FrameworksView(viewsets.ModelViewSet):
queryset = Frameworks.objects.all()
serializer_class = FrameworksSerializer
filter_backends = [SearchFilter,OrderingFilter]
search_fields = ['framework_name']
def get_serializer_class(self):
serializer_class = self.serializer_class
if self.request.method == 'GET':
serializer_class = GetFrameworksSerializer
return serializer_class
这里我正在获取这样的api:
[
{
"language_name": "python",
"created_on": "2018-10-03T04:59:37.407717Z",
"latest_build_on": "2018-10-03T04:59:37.407801Z",
"latest_version": "3.60",
"company": {
"company_id": 1,
"company_name": "Guido van dom rossum",
"started_from": "2018-10-03T04:58:54.889132Z",
"country": "Netherland",
"email": "help@python.com",
"website": "https://python.org",
"ip": "127.0.0.1",
"active": true
},
"technology": [
"blockchain",
"machine learning"
],
"frameworks": [
"django",
"flask",
"bottle"
]
}
]
但是我期望:
[
{
"language_name": "python",
"created_on": "2018-10-03T04:59:37.407717Z",
"latest_build_on": "2018-10-03T04:59:37.407801Z",
"latest_version": "3.60",
"total_technology":2,
"toatl_frameworks": 3,
"company": {
"company_id": 1,
"company_name": "Guido van dom rossum",
"started_from": "2018-10-03T04:58:54.889132Z",
"country": "Netherland",
"email": "help@python.com",
"website": "https://python.org",
"ip": "127.0.0.1",
"active": true
},
"technology": [
"blockchain",
"machine learning"
],
"frameworks": [
"django",
"flask",
"bottle"
]
}
]
我期望在我的api中
"total_technology":2,
"toatl_frameworks": 3,
通过分组计算“技术”和“框架”,这两个额外的字段
请看一下我的代码。
如果是同一型号,则不存在问题。但是在这里我想用foreignkey 所以我很困惑。
答案 0 :(得分:1)
在序列化程序中使用SerializerMethodField()
class GetLanguageSerializer(serializers.ModelSerializer):
technology = serializers.StringRelatedField(many=True)
frameworks = serializers.StringRelatedField(many=True)
total_technology = serializers.SerializerMethodField(read_only=True)
toatl_frameworks = serializers.SerializerMethodField(read_only=True)
def get_toatl_frameworks(self, language):
return language.frameworks.count()
def get_total_technology(self, language):
return language.technology.count() # change 'technology' with corresponding "related_name" value
class Meta:
model = Language
fields = (other_fileds, 'total_technology', 'toatl_frameworks')
depth = 1