简体   繁体   中英

How to write query in django to join two tables?

class Subscriber(Model):

"""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)

class Contact(Model):

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)

These are my two Database which lies in two different Models ..

I want to write query in which subscriber.contact_id=contact.id What should I do to run this query

Given you have contact:

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

you get the subscriber:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM