【发布时间】:2025-12-10 15:35:01
【问题描述】:
我在models.py中有这个类:
class Customer(models.Model):
code = models.CharField(unique=True, max_length=10)
user = models.OneToOneField(User, on_delete=models.PROTECT)
phone = models.CharField(max_length=20)
address = models.TextField(blank=True)
balance = models.PositiveIntegerField(default=20000)
Customer 与 User 类是一对一的关系。 User 类是 Django 中默认的 User 类:
from django.contrib.auth.models import User
我想返回这样的结果:
"customers": [
{
"code": a1,
"username": "arashb91",
"email": "arash@example.com",
"phone": "111111",
"address": "somewhere",
"balance": 20000
},
但是当我使用这段代码时:
result = Customer.objects.all().values("code", 'user__username', 'user__email', "phone", "address", "balance")
我的结果会是这样的:
"customers":
{
"code": a1,
"user__username": "arashb91",
"user__email": "arash@example.com",
"phone": "111111",
"address": "somewhere",
"balance": 20000
},
我可以通过F方法重命名字段:
result = Customer.objects.all().values("code", username=F('user__username'),
email=F('user__email'), "phone", "address", "balance")
但我在“电话”、“地址”、“余额”上收到此错误:
Positional argument after keyword argument
"code" 没问题,因为它在 F 方法之前。 结果顺序对我来说很重要。
我也不能使用此代码(其他字段为 F):
result = Customer.objects.all().values("code", username=F('user__username'),
email=F('user__email'), phone=F("phone"))
因为我收到此错误:
The annotation 'phone' conflicts with a field on the model.
我不希望我的结果是这样的: 其他表__字段
为了解决我的问题,我使用 python 代码重命名字典键,但这不是一个好的选择。 我该怎么办? 有什么简单的解决办法吗?
【问题讨论】:
-
在
values之前使用annotate。 -
非常感谢。我使用了 annotate ,但不是在值之前
-
注释更改顺序:
-
lookups = Customer.objects.all().annotate(username=F("user__username")).values("code", 'username', "balance")) :余额在代码之后结果,不是在用户名之后
标签: python django django-models jsonresponse