如何在django中编写查询来加入两个表?

时间:2016-06-15 08:55:54

标签: python mysql django django-models

类订阅者(模型):

"""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我该怎么做才能运行此查询

1 个答案:

答案 0 :(得分:1)

鉴于您有联系方式:

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

你得到了订阅者:

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