【问题标题】:display records which are in another model in django admin在 django admin 中显示另一个模型中的记录
【发布时间】:2015-03-27 00:31:53
【问题描述】:

我有类似的模型

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 字段不受此数据库后端支持?

那么我怎样才能显示唯一记录?不干扰客户和请求模型?

【问题讨论】:

    标签: mysql django django-admin


    【解决方案1】:

    请看这里:https://docs.djangoproject.com/en/1.7/ref/models/querysets/#django.db.models.query.QuerySet.distinct

    仅在 Postgres 上支持选择 .distinct() 中的字段。

    要使这项工作在其他后端上运行,您可能需要不同的策略。我没有测试过这些,但你可以试试:

    1. .annotate().order_by('field')
    2. .order_by('field').distinct()
    3. 在返回查询集后通过 Python 删除重复项。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-15
      • 1970-01-01
      • 1970-01-01
      • 2021-11-26
      • 1970-01-01
      • 2020-02-03
      • 1970-01-01
      • 2013-08-28
      相关资源
      最近更新 更多