class Tag(models.Model):
label = models.SlugField(null=False, blank=False, unique=True)
# (iii) or here?
# ...
class Entry(models.Model):
title = models.CharField(null=False, blank=False, max_length=128)
content = models.TextField(null=False, blank=False)
slug = models.SlugField(unique=True)
tags = models.ManyToManyField(Tag, null=False, blank=False)
dtime_created = models.DateTimeField(null=False, blank=False, auto_now_add=True)
from blog.models import *
from rest_framework import serializers
class EntrySerializer(serializers.ModelSerializer):
class Meta:
model = Entry
fields = ("title", "content")
class TagSerializer(serializers.ModelSerializer):
class Meta:
model = Tag
fields = ("label")
# (ii) here?
class TagList(APIView):
def search_by_query(self, query):
try:
return Tag.objects.filter(label__icontains=re.escape(query))
# (i) do something here?
except Tag.DoesNotExist:
raise Http404
def get(self, request, format=None):
q = None
try:
q = request.POST["q"]
except KeyError:
return Response(status=status.HTTP_400_BAD_REQUEST)
tags = self.search_by_query(query)
serializer = TagSerializer(tags, many=True)
return Response(serializer.data)
def post(self, request, format=None):
if not request.user.is_authenticated():
return Response(status=status.HTTP_403_FORBIDDEN)
serializer = TagSerializer(request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
我的问题有点复杂。现在,我有一个由Tag
模型使用的Entry
模型。然后,我创建了一个名为APIView
的{{1}},它转储包含标签的字符串。但是,问题是,我还要转储TagList
实例中使用标记的次数,如下所示:
Entry
怎么做?
答案 0 :(得分:1)
非常简单 - 使用DRF的source
核心参数。
在Tag
模型上定义一个计算任何标记出现次数的方法:
class Tag(models.Model):
label = models.SlugField(null=False, blank=False, unique=True)
def nr_of_entries(self):
return self.entry_set.count()
然后在TagSerializer
上添加一个自定义字段,该字段将从您在上面定义的方法中获取数据:
class TagSerializer(serializers.ModelSerializer):
length = serializers.IntegerField(source="nr_of_entries", read_only=True)
class Meta:
model = Tag
fields = ("label", "length")
现在,当序列化tag
的实例时,它将包含length
属性,该属性将从在{{nr_of_entries
方法上运行tag
方法的结果中获取其值1}}实例。