【问题标题】:How to write query in django to join two tables?如何在 django 中编写查询以连接两个表?
【发布时间】:2016-10-16 06:51:12
【问题描述】:

类订阅者(模型):

"""This defines the Contact imported to a Campaign
**Attributes**:
    * ``last_attempt`` - last call attempt date
    * ``count_attempt`` - Count the amount of call attempt
    * ``completion_count_attempt`` - Count the amount of attempt to call in order to achieve completion
    * ``duplicate_contact`` - copy of the contact phonenumber
    * ``status`` - subscriber status
**Relationships**:
    * ``contact`` - Foreign key relationship to the Contact model.
    * ``campaign`` - Foreign key relationship to the Campaign model.
**Name of DB table**: dialer_subscriber
"""
contact = models.ForeignKey(Contact, null=True, blank=True, help_text=_("select contact"))
campaign = models.ForeignKey(Campaign, null=True, blank=True, help_text=_("select campaign"))
last_attempt = models.DateTimeField(null=True, blank=True, verbose_name=_("last attempt"))
count_attempt = models.IntegerField(default=0, null=True, blank=True, verbose_name=_("count attempts"))
# Count the amount of attempt to call in order to achieve completion
completion_count_attempt = models.IntegerField(default=0, null=True, blank=True,
                                               verbose_name=_("completion count attempts"))
# We duplicate contact to create a unique constraint
duplicate_contact = models.CharField(max_length=90, verbose_name=_("contact"))
status = models.IntegerField(choices=list(SUBSCRIBER_STATUS), default=SUBSCRIBER_STATUS.PENDING,
                             verbose_name=_("status"), blank=True, null=True)
disposition = models.IntegerField(verbose_name=_("disposition"), blank=True, null=True)
collected_data = models.TextField(verbose_name=_('subscriber response'), blank=True, null=True,
                                  help_text=_("collect user call data"))
# agent = models.ForeignKey(Agent, verbose_name=_("agent"),
#                          blank=True, null=True,
#                          related_name="agent")

created_date = models.DateTimeField(auto_now_add=True, verbose_name=_('date'))
updated_date = models.DateTimeField(auto_now=True, db_index=True)

类联系人(模型):

phonebook = models.ForeignKey(Phonebook, verbose_name=_('phonebook'))
contact = models.CharField(max_length=90, verbose_name=_('contact number'))
status = models.IntegerField(choices=list(CONTACT_STATUS), default=CONTACT_STATUS.ACTIVE,
                             verbose_name=_("status"), blank=True, null=True)
last_name = models.CharField(max_length=120, blank=True, null=True, verbose_name=_('last name'))
first_name = models.CharField(max_length=120, blank=True, null=True, verbose_name=_('first name'))
email = models.EmailField(blank=True, null=True, verbose_name=_('email'))
address = models.CharField(max_length=250, null=True, blank=True, verbose_name=_("address"))
city = models.CharField(max_length=120, blank=True, null=True, verbose_name=_('city'))
state = models.CharField(max_length=120, blank=True, null=True, verbose_name=_('state'))
country = CountryField(blank=True, null=True, verbose_name=_('country'))
unit_number = models.CharField(max_length=50, blank=True, null=True, verbose_name=_("unit number"))
additional_vars = jsonfield.JSONField(
    null=True, blank=True, verbose_name=_('additional parameters (JSON)'),
    help_text=_("enter the list of parameters in JSON format, e.g. {\"age\": \"32\"}"))
description = models.TextField(null=True, blank=True, verbose_name=_("notes"))
created_date = models.DateTimeField(auto_now_add=True, verbose_name=_('date'))
updated_date = models.DateTimeField(auto_now=True)

这是我的两个数据库,位于两个不同的模型中..

我想在其中编写查询 subscriber.contact_id=contact.id 我应该怎么做才能运行这个查询

【问题讨论】:

  • c.contact,c.address,s.campaign_id,to_char(s.updated_date,'YY:MM:DD HH:MI:ss') from dialer_subscriber s join dialer_contact c on s.contact_id= c.id where s.status=8 and s.updated_date >= current t_date) 到 'New-Today.csv' with CSV;这是我要运行的完整查询
  • 如果你有一些Contactcontact 实例要查询,你可以使用Subscriber.objects.filter(contact=contact)

标签: python mysql django django-models


【解决方案1】:

如果您有联系方式:

contact = Contact.objects.get(id=contact_id)

你得到了订阅者:

subscriber = Subscriber.objects.filter(contact=contact)

【讨论】:

  • 你可以用Subscriber.objects.filter(contact__id=contact_id)在一个班轮中做到这一点
  • 内部服务器错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-18
  • 1970-01-01
  • 2018-10-08
  • 2016-07-14
相关资源
最近更新 更多