Re: Help to implement join query in django orm

2022-07-25 Thread 'Amitesh Sahay' via Django users
10. How to perform join operations in django ORM? — Django ORM Cookbook 2.0 
documentation (agiliq.com)



Try this 

On Monday, 25 July, 2022 at 01:18:44 pm IST, Mihir Patel 
 wrote:  
 
 is anyone having login issue? , i am unable to authenticate a user in my 
website
On Sun, Jul 24, 2022 at 10:37 PM Jitendra kumar Patra 
 wrote:

Ping me 7008080545
On Fri, 22 Jul, 2022, 12:16 Avi shah,  wrote:

I have two tables Tbl 1 & Tbl 2 
I need to connect the two tables using a join 

Thanks and regards, Avi shah 

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CALa7AFNmoep5MT3MwoYn1ZF0S%3DENPUdVAO-_3EmVyAzCub_Jkg%40mail.gmail.com.



-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAOAROf%3DgMPbSQ37f81A0rpRr7NgiUFF8ZyTsF8bZ-KG9Sgh%3DkA%40mail.gmail.com.



-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAC10kRFL%3D1a6T0-DG%3DZTsBugJa2_iFf-itHjSwAc9-2vHOnB7g%40mail.gmail.com.
  

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/1973455060.1368718.1658735565276%40mail.yahoo.com.


Re: Help to implement join query in django orm

2022-07-25 Thread Mihir Patel
is anyone having login issue? , i am unable to authenticate a user in my
website

On Sun, Jul 24, 2022 at 10:37 PM Jitendra kumar Patra <
jitendrapatra...@gmail.com> wrote:

> Ping me 7008080545
>
> On Fri, 22 Jul, 2022, 12:16 Avi shah,  wrote:
>
>> I have two tables
>> Tbl 1
>> &
>> Tbl 2
>>
>> I need to connect the two tables using a join
>>
>>
>> Thanks and regards,
>> Avi shah
>>
>> --
>> 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+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CALa7AFNmoep5MT3MwoYn1ZF0S%3DENPUdVAO-_3EmVyAzCub_Jkg%40mail.gmail.com
>> 
>> .
>>
> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAOAROf%3DgMPbSQ37f81A0rpRr7NgiUFF8ZyTsF8bZ-KG9Sgh%3DkA%40mail.gmail.com
> 
> .
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAC10kRFL%3D1a6T0-DG%3DZTsBugJa2_iFf-itHjSwAc9-2vHOnB7g%40mail.gmail.com.


Re: Help to implement join query in django orm

2022-07-24 Thread Jitendra kumar Patra
Ping me 7008080545

On Fri, 22 Jul, 2022, 12:16 Avi shah,  wrote:

> I have two tables
> Tbl 1
> &
> Tbl 2
>
> I need to connect the two tables using a join
>
>
> Thanks and regards,
> Avi shah
>
> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CALa7AFNmoep5MT3MwoYn1ZF0S%3DENPUdVAO-_3EmVyAzCub_Jkg%40mail.gmail.com
> 
> .
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAOAROf%3DgMPbSQ37f81A0rpRr7NgiUFF8ZyTsF8bZ-KG9Sgh%3DkA%40mail.gmail.com.


Re: Help to implement join query in django orm

2022-07-24 Thread Abhishek Gupta
Hey, you have to write custom raw query for join tables which are not
connected with foreign key relationship in django.

On Sun, Jul 24, 2022, 11:17 Lalit Suthar  wrote:

> this can be helpful
>
> https://betterprogramming.pub/django-select-related-and-prefetch-related-f23043fd635d
>
> On Sun, 24 Jul 2022 at 00:30, Ryan Nowakowski  wrote:
>
>> On Fri, Jul 22, 2022 at 12:16:14PM +0530, Avi shah wrote:
>> > I have two tables
>> > Tbl 1
>> > &
>> > Tbl 2
>> >
>> > I need to connect the two tables using a join
>>
>> If these tables were created outside of Django, in other words, not
>> using manage.py migrate, you can use Django's legacy database support to
>> auto generate the models[1].  After that, each table will have a model
>> associated with it.  You can then use Django's ForeignKey support to query
>> the tables.
>>
>>
>> [1] https://docs.djangoproject.com/en/4.0/howto/legacy-databases/
>>
>>
>> --
>> 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+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/20220723185935.GB15838%40fattuba.com
>> .
>>
> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAGp2JVHauj%2BS47HfmcYOrNQHJk3bTQbtdBm%3DdLRNx_8woM6XwQ%40mail.gmail.com
> 
> .
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CABwiCvrSuY6AmBkARJFt%2BaCeR_wFVGf%3DKWdLP227GApDW4AcWQ%40mail.gmail.com.


Re: Help to implement join query in django orm

2022-07-23 Thread Lalit Suthar
this can be helpful
https://betterprogramming.pub/django-select-related-and-prefetch-related-f23043fd635d

On Sun, 24 Jul 2022 at 00:30, Ryan Nowakowski  wrote:

> On Fri, Jul 22, 2022 at 12:16:14PM +0530, Avi shah wrote:
> > I have two tables
> > Tbl 1
> > &
> > Tbl 2
> >
> > I need to connect the two tables using a join
>
> If these tables were created outside of Django, in other words, not
> using manage.py migrate, you can use Django's legacy database support to
> auto generate the models[1].  After that, each table will have a model
> associated with it.  You can then use Django's ForeignKey support to query
> the tables.
>
>
> [1] https://docs.djangoproject.com/en/4.0/howto/legacy-databases/
>
>
> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/20220723185935.GB15838%40fattuba.com
> .
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAGp2JVHauj%2BS47HfmcYOrNQHJk3bTQbtdBm%3DdLRNx_8woM6XwQ%40mail.gmail.com.


Re: Help to implement join query in django orm

2022-07-23 Thread Ryan Nowakowski
On Fri, Jul 22, 2022 at 12:16:14PM +0530, Avi shah wrote:
> I have two tables
> Tbl 1
> &
> Tbl 2
> 
> I need to connect the two tables using a join

If these tables were created outside of Django, in other words, not
using manage.py migrate, you can use Django's legacy database support to
auto generate the models[1].  After that, each table will have a model
associated with it.  You can then use Django's ForeignKey support to query
the tables.


[1] https://docs.djangoproject.com/en/4.0/howto/legacy-databases/
 

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/20220723185935.GB15838%40fattuba.com.


Help to implement join query in django orm

2022-07-21 Thread Avi shah
I have two tables
Tbl 1
&
Tbl 2

I need to connect the two tables using a join


Thanks and regards,
Avi shah

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CALa7AFNmoep5MT3MwoYn1ZF0S%3DENPUdVAO-_3EmVyAzCub_Jkg%40mail.gmail.com.


Re: anti-join query in Django

2011-11-21 Thread Russell Keith-Magee
On Tue, Nov 22, 2011 at 1:39 AM, dmitry b  wrote:
> Hi Russ,
>
> Thanks for a quick response.
>
> I don't think Django needs to make the decision of whether or not to
> do a join.  That can be done by the underlying database as long as
> Django generates a query that the database can understand and optimize
> properly.

This assumes that databases are equally good at performing
optimizations of this kind. It's true that PostgreSQL does, and I
believe that Oracle does as well; however, MySQL and SQLite are not
equally blessed (to the best of my knowledge).

Yours,
Russ Magee %-)

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: anti-join query in Django

2011-11-21 Thread dmitry b
Hi Russ,

Thanks for a quick response.

I don't think Django needs to make the decision of whether or not to
do a join.  That can be done by the underlying database as long as
Django generates a query that the database can understand and optimize
properly.   In this case, it would imply that Django would still need
to generate a join, but check the null condition not on the primary
key field but on one of the fields the join condition.

Regardless, I'm going to file a ticket on this.

Thanks again.
D.

On Nov 19, 2:36 am, Russell Keith-Magee 
wrote:
> On Sat, Nov 19, 2011 at 8:41 AM, dmitry b  wrote:
> > Is there a recommended approach to anti-join queries?  Here's the
> > query I'm having an issue with:
>
> > Branch.objects.filter(branchgroup__isnull=True)[:1]
>
> > where branchgroup is a ManytoMany relationship to another model.  I
> > want to get a set of Branch objects that aren't mapped to any
> > BranchGroups.
>
> Hi Dmitry,
>
> There isn't any specific way to force the non-creation of a join
> condition; Django doesn't expose the internals of query construction,
> and should be choosing the optimal SQL for the ORM query.
>
> The sort of optimization you describe (i.e., don't do the join if you
> can just check the primary key value on the local model) is an
> optimization that Django is definitely capable of performing. If the
> ORM isn't performing this optimization in this case, it's possible
> you've found a bug.
>
> There are some cases where the optimization *isn't* possible, but
> without seeing the models for your test case, it's impossible to say
> for certain.
>
> If you think you've found a way that Django could optimize it's
> queries better, the best way forward is to open a ticket, with a set
> of sample models and the expected/actual query that is generated for
> those specific models.
>
> Yours,
> Russ Magee %-)

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: anti-join query in Django

2011-11-19 Thread Russell Keith-Magee
On Sat, Nov 19, 2011 at 8:41 AM, dmitry b  wrote:
> Is there a recommended approach to anti-join queries?  Here's the
> query I'm having an issue with:
>
> Branch.objects.filter(branchgroup__isnull=True)[:1]
>
> where branchgroup is a ManytoMany relationship to another model.  I
> want to get a set of Branch objects that aren't mapped to any
> BranchGroups.

Hi Dmitry,

There isn't any specific way to force the non-creation of a join
condition; Django doesn't expose the internals of query construction,
and should be choosing the optimal SQL for the ORM query.

The sort of optimization you describe (i.e., don't do the join if you
can just check the primary key value on the local model) is an
optimization that Django is definitely capable of performing. If the
ORM isn't performing this optimization in this case, it's possible
you've found a bug.

There are some cases where the optimization *isn't* possible, but
without seeing the models for your test case, it's impossible to say
for certain.

If you think you've found a way that Django could optimize it's
queries better, the best way forward is to open a ticket, with a set
of sample models and the expected/actual query that is generated for
those specific models.

Yours,
Russ Magee %-)

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



anti-join query in Django

2011-11-18 Thread dmitry b
Is there a recommended approach to anti-join queries?  Here's the
query I'm having an issue with:

Branch.objects.filter(branchgroup__isnull=True)[:1]

where branchgroup is a ManytoMany relationship to another model.  I
want to get a set of Branch objects that aren't mapped to any
BranchGroups.  This query gets translated to the following (a
simplified equivalent):


SELECT "scm_branch"."id"
   FROM "scm_branch"
  LEFT OUTER JOIN "scm_branchgroup_branches"
 ON ("scm_branch"."id" =
"scm_branchgroup_branches"."branch_id")
  LEFT OUTER JOIN "scm_branchgroup"
 ON ("scm_branchgroup_branches"."branchgroup_id" =
"scm_branchgroup"."id")
   WHERE "scm_branchgroup"."id" IS NULL
   LIMIT 1

This query is very slow:  Limit  (cost=1072479.36..6256437.79 rows=1
width=145)

However, a slightly modified, but functionally equivalent query:

SELECT "scm_branch"."id"
   FROM "scm_branch"
  LEFT OUTER JOIN "scm_branchgroup_branches"
 ON ("scm_branch"."id" =
"scm_branchgroup_branches"."branch_id")
  LEFT OUTER JOIN "scm_branchgroup"
 ON ("scm_branchgroup_branches"."branchgroup_id" =
"scm_branchgroup"."id")
   WHERE "scm_branchgroup_branches"."branch_id" IS NULL
   LIMIT 1

Is orders of magnitude faster:  Limit  (cost=1518.71..1533.35 rows=1
width=145)

The difference is with the WHERE clause.  Django generates WHERE
"scm_branchgroup"."id" IS NULL, but a properly optimized query should
use  WHERE "scm_branchgroup_branches"."branch_id" IS NULL.  This is
because Postgres recognizes the second query as a anti-join query and
can do a lot of optimization.

The basic question is: How do I make Django generate a faster query?


Thanks
Dmitry

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: "join" query in Django?

2009-07-31 Thread Javier Guerra

On Fri, Jul 31, 2009 at 1:42 AM, Asinox wrote:
> i try
> with .filter("field1__field2") where the field1 is the PK and the
> field2 is the FK.

that dowsn't sound right.  i think you've misread the ORM chapters of
the documentation.

don't try to think in terms of the SQL you want.  much better is to
think in terms of the data you want from the DB.

-- 
Javier

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: "join" query in Django?

2009-07-31 Thread Malcolm Tredinnick

On Thu, 2009-07-30 at 23:42 -0700, Asinox wrote:
> Thanks Malcom for replay, but, is not working for me, i think that is
> simple just .filter("field1__field2"), but i cant make the join i try
> with .filter("field1__field2") where the field1 is the PK and the
> field2 is the FK.
> 
> here is my query:
> 
> p = Diligencia.objects.filter(Q(socio=request.user.id),Q(status=0) | Q
> (status=1)| Q(status=3)).filter("id__diligencia")
> 
> and the error is: too many values to unpack

It's effectively impossible to debug this from the information you've
provided, because we cannot see the model(s) you are trying to query
against. I am, however, very suspicious of the bit that says
filter("id__diligencia"), since "id" is almost always the auto-generated
primary key field and not a reference to another model.

Please construct some simple models that show the problem you're seeing.
Also, the error isn't just reported as you say above. It also has a
traceback in there, which is typically very useful, as it shows us where
the error is coming from.

> 
> im new with Django i cant understand this error, i searched and i cant
> find answer about it.
> 
> The another try was:
> 
> p = Diligencia.objects.filter(Q(socio=request.user.id),Q(status=0) | Q
> (status=1)| Q(status=3)).select_related()
> 
> in this case i dont have errors but the data that i want to show from
> the second table is not showing... and Django dont have any setting
> function that will show the "SQL" that is happening in any query,

You can try this approach, which shows the SQL after the query has been
run:

http://docs.djangoproject.com/en/dev/faq/models/#how-can-i-see-the-raw-sql-queries-django-is-running

Or look at the output of p.query.as_sql() (where "p" is your queryset,
above) to see the results of the SQL that would be sent to the database.
Or look at django-debug-toolbar for a more in-browser view of thigns.

Regards,
Malcolm



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: "join" query in Django?

2009-07-30 Thread Asinox

Thanks Malcom for replay, but, is not working for me, i think that is
simple just .filter("field1__field2"), but i cant make the join i try
with .filter("field1__field2") where the field1 is the PK and the
field2 is the FK.

here is my query:

p = Diligencia.objects.filter(Q(socio=request.user.id),Q(status=0) | Q
(status=1)| Q(status=3)).filter("id__diligencia")

and the error is: too many values to unpack

im new with Django i cant understand this error, i searched and i cant
find answer about it.

The another try was:

p = Diligencia.objects.filter(Q(socio=request.user.id),Q(status=0) | Q
(status=1)| Q(status=3)).select_related()

in this case i dont have errors but the data that i want to show from
the second table is not showing... and Django dont have any setting
function that will show the "SQL" that is happening in any query, like
CodeIgniter "Profiler APP", that show you the SQL and you will see if
the query is selecting the data that you want... im lost with
Django...i missing PHP and Codeigniter :(, but i want to learn Django

Im sorry with my english and my errors with djando :(
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: "join" query in Django?

2009-07-30 Thread Malcolm Tredinnick

On Thu, 2009-07-30 at 21:23 -0700, Asinox wrote:
> Hi, guys, how or where is the "join" query in Django?
> 
> i think that Django dont have "join"..but how ill make  join?

SQL-level joins happen automatically when required. You specify your
queryset in terms of filter() and exclude() calls and Django works out
which tables are needed to compute that result. Links between models are
specified using the double-underscore notation. Thus, "foo__bar" means
to use the "foo" field on the current model (a link to a related model)
and follow that to the "bar" field on the other end of the relation.

Regards,
Malcolm


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



"join" query in Django?

2009-07-30 Thread Asinox

Hi, guys, how or where is the "join" query in Django?

i think that Django dont have "join"..but how ill make  join?

Thanks
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---