Django: how to make queries between 2 tables that are not related in the model?

34 views
Skip to first unread message

Jérôme Le Carrou

unread,
Feb 13, 2020, 4:28:40 AM2/13/20
to Django users
Hi,

I have 2 models that are not related (maybe it should...)

I need to make a query that select all records of my table Stock WITH related rows in Parametrage based on asp_sto_loc=asp_par_loc

Something like SQL: select * from pha_asp_sto left join pha_asp_par on pha_asp_par.asp_par_loc=asp_sto_loc;

How can I make such a query in Django?
I have see extra() but seems to be deprecated...
Another option would be to use raw() but not recommended...

class Stock(models.Model):

    asp_sto_cle = models.AutoField(primary_key=True)
    asp_sto_loc = models.CharField("Site concerned", max_length=10, null=True, blank=True)
    asp_sto_pla = models.IntegerField("Quantity of placebos available", null=True, blank=True,)
    asp_sto_asp = models.IntegerField("Quantity of aspirin available", null=True, blank=True)

class Parametrage(models.Model):

    asp_par_cle = models.AutoField(primary_key=True)
    asp_par_loc = models.CharField("Site concerned by settings", max_length=10, null=True, blank=True)
    asp_par_ale = models.IntegerField("Site alert value for the site", null=True, blank=True,)
    asp_par_con = models.IntegerField("Site confort value for the site", null=True, blank=True,)

thanks for your help

maninder singh Kumar

unread,
Feb 13, 2020, 7:56:15 AM2/13/20
to django...@googlegroups.com
You of course mean, no foreign key ?
Are the models pre written so no change can be made to them.

Sent from my iPad
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/029c93e0-4093-4a4f-bc99-b3af000c3c31%40googlegroups.com.

onlinejudge95

unread,
Feb 13, 2020, 9:00:21 AM2/13/20
to django...@googlegroups.com
On Thu, Feb 13, 2020 at 2:59 PM Jérôme Le Carrou <j.lec...@gmail.com> wrote:
Hi,

I have 2 models that are not related (maybe it should...)

I need to make a query that select all records of my table Stock WITH related rows in Parametrage based on asp_sto_loc=asp_par_loc

Something like SQL: select * from pha_asp_sto left join pha_asp_par on pha_asp_par.asp_par_loc=asp_sto_loc;
 
How can I make such a query in Django?
I have see extra() but seems to be deprecated...
Another option would be to use raw() but not recommended...

class Stock(models.Model):

    asp_sto_cle = models.AutoField(primary_key=True)
    asp_sto_loc = models.CharField("Site concerned", max_length=10, null=True, blank=True)
    asp_sto_pla = models.IntegerField("Quantity of placebos available", null=True, blank=True,)
    asp_sto_asp = models.IntegerField("Quantity of aspirin available", null=True, blank=True)

class Parametrage(models.Model):

    asp_par_cle = models.AutoField(primary_key=True)
    asp_par_loc = models.CharField("Site concerned by settings", max_length=10, null=True, blank=True)
    asp_par_ale = models.IntegerField("Site alert value for the site", null=True, blank=True,)
    asp_par_con = models.IntegerField("Site confort value for the site", null=True, blank=True,)

thanks for your help

--

Mehul Anshumali

unread,
Feb 13, 2020, 9:09:09 AM2/13/20
to django...@googlegroups.com
Connect one table with another table using foreign key and then in command prompt type python manage.py shell and then import your table in shell then write queries .

--
Reply all
Reply to author
Forward
0 new messages