gpt4 book ai didi

django - 如何为连接模型编写序列化器?

转载 作者:行者123 更新时间:2023-12-02 22:19:06 26 4
gpt4 key购买 nike

我已使用以下查询对两个表应用了联接,

VIEWS.PY

class performance(viewsets.ModelViewSet):

queryset = Leads.objects.select_related('channelId'
).values("channelId__channelName").annotate(tcount=Count('channelId'))

serializer_class = teamwise_lead_performance_serializer

但我无法使用此序列化器捕获响应,

序列化器.PY

class channel_serializer(serializers.ModelSerializer):
class Meta:
model = Channels
fields = ['channelName']


class performance_serializer(serializers.ModelSerializer):
tcount = serializers.IntegerField()
channel = channel_serializer(many=True, read_only=True)

class Meta:
model = Leads
fields = ['tcount', 'channel']

实际结果:

[
{
"tcount": 88
},
{
"tcount": 25
},
{
"tcount": 31
},
...
]

预期结果:

[
{
"channelName": "abc",
"tcount": 88
},
{
"channelName": "def",
"tcount": 25
},
{
"channelName": "ghi",
"tcount": 31
},
...
]

我尝试过以下方法:

How to join two models in django-rest-framework

模型.py

class Channels(models.Model):
id = models.IntegerField(primary_key=True)
channelName = models.CharField(max_length=20, default=None)

class Meta:
db_table = "table1"

class Leads(models.Model):
id = models.IntegerField(primary_key=True)
channelId = models.ForeignKey(Channels, on_delete=models.CASCADE, db_column='channelId')

class Meta:
db_table = "table2"

为什么没有得到 channelName 响应?我在这里做错了什么?谢谢您的建议

编辑

当我尝试 Mehren 的答案时,出现以下错误:

KeyError when attempting to get a value for field channelName on serializer performance_serializer. The serializer field might be named incorrectly and not match any attribute or key on the dict instance.Original exception text was: 'channelId'.

最佳答案

我设法让它与以下内容一起工作:

class performance_serializer(serializers.ModelSerializer):
tcount = serializers.IntegerField()
channelName = serializers.CharField(source='channelId__channelName')

class Meta:
model = Leads
fields = ['tcount', 'channelName']

class performance(viewsets.ModelViewSet):
queryset = Leads.objects.select_related('channelId'
).values("channelId__channelName").annotate(tcount=Count('channelId'))
serializer_class = performance_serializer

话虽这么说,我强烈鼓励您同时关注 PEPDjango命名约定。

以下是遵循上述约定的代码:

class Channel(models.Model):
id = models.IntegerField(primary_key=True)
channel_name = models.CharField(max_length=20, default=None)

class Lead(models.Model):
id = models.IntegerField(primary_key=True)
channel = models.ForeignKey(Channel, on_delete=models.CASCADE)

class PerformanceSerializer(serializers.ModelSerializer):
channel_count = serializers.IntegerField()
channel_name = serializers.CharField(source='channel__channel_name')

class Meta:
model = Lead
fields = ['channel_count', 'channel_name']

class PerformanceViewSet(viewsets.ModelViewSet):
queryset = Lead.objects.select_related('channel'
).values("channel__channel_name").annotate(channel_count=Count('channel'))
serializer_class = PerformanceSerializer

这样做的主要要点是不要更改 ForeignKey 列的默认名称!它使使用相关模型变得更加困惑,并且可能是您问题的首要原因(尽管我无法证明这一点)。

关于django - 如何为连接模型编写序列化器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58178921/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com