我有这样的模型
class Customers(models.Model):
m1 = models.EmailField()
class Requests(models.Model):
m2 = models.EmailField()
class CustomersRequests(Customers):
class Meta:
proxy = True
我的问题是我想在 CustomersRequests 管理中显示唯一的电子邮件,以便在请求模型中显示这些客户电子邮件。
例如:
客户
v1@xyz.com
v2@xyz.com
v3@xyx.com
请求
v1@xyz.com
v1@xyz.com
v3@xyx.com
v1@xyz.com
我只想要 CustomersRequests 中的以下记录
v1@xyz.com
v3@xyx.com
我在 CustomersRequests 中将查询集写入不同的记录,但 mysql 数据库给出了该数据库后端不支持 DISTINCT ON 字段?
那么如何显示唯一记录呢?不干扰客户和请求模型?
最佳答案
请参见此处:https://docs.djangoproject.com/en/1.7/ref/models/querysets/#django.db.models.query.QuerySet.distinct
仅 Postgres 支持选择 .distinct()
内的字段。
要使其在其他后端上工作,您可能需要不同的策略。我还没有测试过这些,但你可以尝试:
.annotate().order_by('field')
.order_by('field').distinct()
- 返回查询集后通过 Python 删除重复项。
关于mysql - 显示 django admin 中另一个模型中的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28186648/