1

Since I am new to django, I know how to query a single model in Django but I need to query multiple models at the same time.

I need to develop a UNION query in Django with 3 models namely WebQuery,WebReply and BusinessOwners and the output should be of the form below.

{
    "(#conversation_id#)_(#b_id#)": {
        "from": "(#user_id)",
        "email": "(#user_email)",
        "date_time": "#get from db",
        "query": "are you open ?",
        "from_r_id": "(#representative_id)",
        "from_r_name": "(#rep_name)",
        "business_registered": "FALSE"
        "to_business_name": "CCD saket",
        "chat": [{
            "direction": 1,
            "text": "yes sir",
            "date_time": "424 577"
        }, {
            "direction": 0,
            "text": "ok",
            "date_time": "424 577"
        }]
    },

Here is my models.py file.

class Businessowners(models.Model):
    b_id = models.IntegerField()
    userid = models.IntegerField(primary_key=True, db_column='UserID')
    email = models.CharField(max_length=150L)
    b_name = models.CharField(max_length=100L)
    class Meta:
        db_table = 'businessowners'

class Users(models.Model):
    user_id = models.BigIntegerField(primary_key=True)
    username = models.CharField(max_length=20L)
    email = models.CharField(max_length=50L)
    date_time = models.DateTimeField()
    class Meta:
        db_table = 'users'

class WebQuery(models.Model):
    query_id = models.BigIntegerField(unique=True)
    conversation_id = models.CharField(max_length=50L)
    u_id = models.CharField(max_length=50L)
    u_query = models.CharField(max_length=500L)
    sent_to = models.CharField(max_length=500L)
    date_time = models.DateTimeField()
    is_reply = models.IntegerField()
    is_responded = models.IntegerField()
    class Meta:
        db_table = 'web_query'

class WebReply(models.Model):
    reply_id = models.IntegerField(primary_key=True)
    query_id = models.BigIntegerField()
    conversation_id = models.CharField(max_length=50L)
    b_id = models.CharField(max_length=20L)
    u_query = models.CharField(max_length=500L)
    user_id = models.CharField(max_length=20L)
    date_time = models.DateTimeField()
    class Meta:
        db_table = 'web_reply'
9
  • If you are generating JSON, you probably want to take a look at the Python Rest Framework and their serialization classes. Commented Jul 16, 2013 at 5:47
  • No. I'm not. Any further assistance please. Thanks. Commented Jul 16, 2013 at 6:07
  • Your sample for the desired output looks like valid JSON to me. Commented Jul 16, 2013 at 6:29
  • What if I just want to query those 3 models, apply some loops in it thereby getting the desired O/P.? Commented Jul 16, 2013 at 6:41
  • what I want to know is how to query 3 models simultaneously, ie , how to apply union query in those 3 models? Commented Jul 16, 2013 at 6:42

1 Answer 1

0

This question is so similar to another which has recently been asked that I recommend you taking a look at Django Union Query.

Basically the way forwards would be to use SQL views if all the sources can be gathered. Then query that view.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.