[英]How to get distinct list of names from a consultation list, and show only latest consultation in Django?
What I want to accomplish is to get a unique list of the names of customers with their lastest consultation date.我想要完成的是获得一个唯一的客户姓名列表以及他们的最新咨询日期。
I have defined these models in my models.py file, using mySQL as my database:我在我的 models.py 文件中定义了这些模型,使用 mySQL 作为我的数据库:
class Customer(models.Model):
class ContactChoice(models.IntegerChoices):
DO_NOT_CONTACT = 0
EMAIL = 1
TXT_SMS_VIBER = 2
mobile_num = models.CharField('Mobile Number', max_length=10, unique=True,)
email_add = models.EmailField('Email', max_length=150, unique=True,)
last_name = models.CharField('Last Name', max_length=30,)
first_name = models.CharField('First Name', max_length=30,)
contact_for = models.CharField('Contact For', max_length=60,)
contact_on = models.IntegerField('Contact Using', choices=ContactChoice.choices, default=0,)
customer_consent = models.BooleanField('With Consent?', default=False,)
def __str__(self):
return self.last_name + ', ' + self.first_name
class Consultation(models.Model):
consultation_date = models.DateTimeField('Date of Consultation', default=now)
customer = models.ForeignKey(Customer, on_delete=models.SET_DEFAULT, default=1)
concern = models.ForeignKey(SkinConcern, on_delete=models.SET_DEFAULT, default=1)
consultation_concern = models.CharField('Other Concerns', max_length=120, null=True,)
product = models.ForeignKey(Product, on_delete=models.SET_DEFAULT, default=1)
user = models.ForeignKey(User, on_delete=models.SET_DEFAULT, default=1)
store = models.ForeignKey(Store, on_delete=models.SET_DEFAULT, default=1)
consultation_is_active = models.BooleanField('Is Active', default=True)
def __str__(self):
return self.customer.last_name + ", " + self.customer.first_name
In my views.py, I have this for the Consultations page:在我的 views.py 中,我有这个用于咨询页面:
distinct = Consultation.objects.values('customer').annotate(consultation_count=Count('customer')).filter(consultation_count=1)
consults = Consultation.objects.filter(customer__in=[item['customer'] for item in distinct])
As mentioned, I was expecting to get a unique list of customer names with their latest consultation dates.如前所述,我希望获得一份独特的客户名单及其最新咨询日期。 This code results in only 1 record being shown.
此代码导致仅显示 1 条记录。
Can you point me in the right direction for this?你能为此指出正确的方向吗? Thank you in advance: :)
先感谢您: :)
As I see it, what you're doing right now is gathering all the customers that only had one consultation.在我看来,你现在正在做的是收集所有只接受过一次咨询的客户。 This won't return what you want.
这不会返回你想要的。
I believe you can use the latest()
method for this: https://docs.djangoproject.com/en/4.1/ref/models/querysets/#latest我相信你可以为此使用
latest()
方法: https://docs.djangoproject.com/en/4.1/ref/models/querysets/#latest
This is untested code, but you could do something like this:这是未经测试的代码,但您可以这样做:
# gather all the customers
customers = Customer.objects.all()
# for every customer, get their latest consultation date, using the .latest() function
for customer in customers:
try:
latest_consultation = Consultation.objects.filter(customer=customer).latest('consultation_date')
latest_consultation_date = latest_consultation.consultation_date
except Consultation.DoesNotExist:
latest_consultation_date = None
customer.latest_consultation_date = latest_consultation_date
you can then loop over it as so:然后你可以循环遍历它:
for customer in customers:
if customer.latest_consultation_date:
print(customer.latest_consultation_date)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.