django objects.filter(x__in = y) 其中 y 是重复的 QuerySet
django objects.filter(x__in = y) where y is repetitive QuerySet
Customer.objects.filter(customer_id__in=contact_list_senders)
contact_list_senders 是一个重复的 QuerySet,涉及一些 customer_ids:
{sender_id1, sender_id2, sender_id1, sender_id2, sender_id1}
当我尝试使用上述代码从 contact_list_senders QuerySet 中查找 Customer 对象时
实际输出:
{Customer1, Customer2}
期望的输出
{Customer1, Customer2, Customer1, Customer2, Customer1}
我知道实际输出是有意义的,因为只有 2 个客户对象与这些联系人匹配。你能帮我得到想要的结果吗?
models.py:
class Customer(models.Model):
customer_id = models.CharField(max_length=244, blank=False, null=False)
first_name = models.CharField(max_length=244, blank=True, null=True)
last_name = models.CharField(max_length=244, blank=True, null=True)
email = models.CharField(max_length=244, blank=False, null=False)
enrollment_method = models.CharField(max_length=244, blank=True, null=False)
account_balance = models.DecimalField(default=0000.00, max_digits=6, decimal_places=2)
reserved_balance = models.DecimalField(default=0000.00, max_digits=6, decimal_places=2)
modified = models.DateTimeField(auto_now_add=True)
created = models.DateTimeField(auto_now_add=True)
你有一个好的开始,但你必须在 python 中完成剩下的工作:
customers = {x.customer_id: x for x in Customer.objects.filter(customer_id__in=contact_list_senders)}
final_customer_list = [customers[x] for x in contact_list_senders]
这首先构建一个将客户 ID 映射到客户的字典(无需对每个客户进行数据库查询),然后使用它来构建最终列表,包括重复项。
假设您的第一个列表是客户 ID 列表,但如果不是,则可以轻松调整。
我在您的 table 上没有看到任何主索引,我想 customer_id 字段应该是一个。在那种情况下,如果您有两个具有多对一关系的模型(许多发件人可以与一个客户相关),那么模型应该看起来像这样:
class Sender(models.Model):
customer = models.ForeignKey(Customer)
# .. and the other fields od a sender
class Customer(models.Model):
customer_id = models.CharField(max_length=244, blank=False, null=False, primary_key=True)
# .. and the other fields of a customer
然后是一个查询集:
# Replace all() with filter(your_params)
sender_list = Sender.objects.select_related('customer').all()
your_desired_output = [sender.customer for sender in sender_list]
selected_related 方法告诉 Django 遵循关系并在同一查询中获取相关的客户对象(以避免 运行 对每个单独的查询遍历查询集时的发件人)。在大多数数据库中,这意味着单个 select 与客户的左连接,因此它会非常有效。
我相信在 Django 中这样做是正确的方法。如果您不使用主键是有原因的 and/or 您无法在模型之间建立这种关系,那么您需要尝试 user6731765 发送的解决方案或尝试原始查询。
Customer.objects.filter(customer_id__in=contact_list_senders)
contact_list_senders 是一个重复的 QuerySet,涉及一些 customer_ids:
{sender_id1, sender_id2, sender_id1, sender_id2, sender_id1}
当我尝试使用上述代码从 contact_list_senders QuerySet 中查找 Customer 对象时
实际输出:
{Customer1, Customer2}
期望的输出
{Customer1, Customer2, Customer1, Customer2, Customer1}
我知道实际输出是有意义的,因为只有 2 个客户对象与这些联系人匹配。你能帮我得到想要的结果吗?
models.py:
class Customer(models.Model):
customer_id = models.CharField(max_length=244, blank=False, null=False)
first_name = models.CharField(max_length=244, blank=True, null=True)
last_name = models.CharField(max_length=244, blank=True, null=True)
email = models.CharField(max_length=244, blank=False, null=False)
enrollment_method = models.CharField(max_length=244, blank=True, null=False)
account_balance = models.DecimalField(default=0000.00, max_digits=6, decimal_places=2)
reserved_balance = models.DecimalField(default=0000.00, max_digits=6, decimal_places=2)
modified = models.DateTimeField(auto_now_add=True)
created = models.DateTimeField(auto_now_add=True)
你有一个好的开始,但你必须在 python 中完成剩下的工作:
customers = {x.customer_id: x for x in Customer.objects.filter(customer_id__in=contact_list_senders)}
final_customer_list = [customers[x] for x in contact_list_senders]
这首先构建一个将客户 ID 映射到客户的字典(无需对每个客户进行数据库查询),然后使用它来构建最终列表,包括重复项。
假设您的第一个列表是客户 ID 列表,但如果不是,则可以轻松调整。
我在您的 table 上没有看到任何主索引,我想 customer_id 字段应该是一个。在那种情况下,如果您有两个具有多对一关系的模型(许多发件人可以与一个客户相关),那么模型应该看起来像这样:
class Sender(models.Model):
customer = models.ForeignKey(Customer)
# .. and the other fields od a sender
class Customer(models.Model):
customer_id = models.CharField(max_length=244, blank=False, null=False, primary_key=True)
# .. and the other fields of a customer
然后是一个查询集:
# Replace all() with filter(your_params)
sender_list = Sender.objects.select_related('customer').all()
your_desired_output = [sender.customer for sender in sender_list]
selected_related 方法告诉 Django 遵循关系并在同一查询中获取相关的客户对象(以避免 运行 对每个单独的查询遍历查询集时的发件人)。在大多数数据库中,这意味着单个 select 与客户的左连接,因此它会非常有效。
我相信在 Django 中这样做是正确的方法。如果您不使用主键是有原因的 and/or 您无法在模型之间建立这种关系,那么您需要尝试 user6731765 发送的解决方案或尝试原始查询。