Re: Problem with sending django channels event from models

2018-07-27 Thread Andrew Godwin
You need to post the full traceback, not just the last section. I suspect
what you are doing is calling model methods directly in an async function,
but I can't confirm that without a full traceback.

Andrew

On Thu, Jul 26, 2018 at 1:10 PM luan fonceca  wrote:

> Hello, i'm trying to implement a "post_save" signal wherever some user are
> update. I setup everything by following the documentation and some
> tutorials for Channels 2. I wrote a test using "py.test" to assert that
> everything is working as expected but i'm getting a exception
> 
> .
>
> On this link i have posted a gist.
> 
>
> Thanks in advance.
>
> --
> 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 post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/3f42ad5c-c737-4a5e-baad-54023261d7f5%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uomro%2B1-qkGC8ThJw6ehz%3DHTenXwUqx-xM%3DkzzTqNgQdg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: User Authenticated in Gunicorn and Daphne

2018-07-27 Thread Andrew Godwin
You can configure Nginx to route to different backends based on path - put
your Nginx proxy_pass settings inside a "location" block.

You can find examples on Stack Overflow - for example:
https://stackoverflow.com/questions/13399252/nginx-reverse-proxy-to-multiple-backends

Andrew

On Tue, Jul 24, 2018 at 1:09 PM  wrote:

> Hi there,
>
> ideally I would like to run Daphne alongside Gunicorn and not Daphne alone:
>
> - My chat with Daphne
> - Rest of the website with Gunicorn
>
> A user logs in - request via Gunicorn. Now when he connects with the chat
> I get an AnonymousUser using self.scope['user']. This is a problem because
> I would like to assign him to the right ChatRoom.
> If I run all requests through Daphne everything works fine.
>
> Also I am using Nginx, don't know if it matters, just gonna leave it here.
>
> How could I fix this? What would be the workaround?
>
> Let me know if you need any code/files.
>
> Best Regards,
>
> Dario
>
> --
> 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 post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/cb2a7475-69dd-4a09-ba32-ea89950d5fc7%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1uo8w8jSe2bALJKfBwzmmLN9VtJwcARBGRhzc%3D%3DQy%2B2AdA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Optimizing Prefetch for Postgres IN Limit

2018-07-27 Thread Ram J
Hi Xof,

I realized what you meant regd the optimizer and understood why my query
was doing a full table scan. The issue was that column through which I
prefetching has a lot of NULLs so we have a partial index on NOT NULL and
the optimizer is getting fooled by that. Adding a IS NOT NULL in the same
query makes the optimizer use the index again.

Thanks
Ram


On Thu, Jul 26, 2018 at 10:09 PM Ram J  wrote:

> Hi Xof,
>
> The issue is that, when you do a naive Prefetch you are left with nothing
> but Django’s auto generated IN query. I was asking about he recommended way
> to make this into a JOIN, which is not obvious how to do with Django
>
> Also in my case the naive IN query on the PK *did not* generate a Index
> scan and ended up being a full table scan
>
> I could have however used a Prefetch object to force PG to use a different
> index apart from the PK but a naive Prefetch of the form
> queryset.prefetch(‘table__table2’) does not use any index beyond 100 values
>
> Also it’s hard to estimate how many values will be there in a nested
>  prefetch, to state the obvious, to do anything different on a query by
> query basis
>
>
> On Thu, Jul 26, 2018 at 19:35 Christophe Pettus  wrote:
>
>>
>> > On Jul 25, 2018, at 02:59, Jason  wrote:
>> >
>> > Where do you get that in the pg documentation? I can't find that
>> anywhere (google-fu may be failing me), and we do have some queries with
>> more than 100 values using IN.
>>
>> It's slightly more complicated than that.  Above 100 entries, the
>> PostgreSQL optimizer won't try to do optimizations of the form changing i
>> IN (1, 2 ..., 101) to (i = 1) OR (i = 2) OR  (i = 3)... to see if there's a
>> better way of executing the query.  It *can* still do an index scan in
>> those cases, although the more entries in the IN list, the less efficient
>> that will be.  In general, large IN clauses aren't a great idea; they're
>> better replaced with a join.
>>
>> --
>> -- Christophe Pettus
>>x...@thebuild.com
>>
>> --
>> You received this message because you are subscribed to a topic in the
>> Google Groups "Django users" group.
>> To unsubscribe from this topic, visit
>> https://groups.google.com/d/topic/django-users/y9sVr9Pbr-o/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to
>> django-users+unsubscr...@googlegroups.com.
>> To post to this group, send email to django-users@googlegroups.com.
>> Visit this group at https://groups.google.com/group/django-users.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/8A21227D-94A9-459F-89D0-771D3052C0C0%40thebuild.com
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>

-- 
Ram J

-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAAW8NznTX9%3DSBG7oAdwuxdw%2B_uryPxfRDMAL5a5nc59SBP9EKw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Change the default route of a view

2018-07-27 Thread Fernando Miranda
Hi Andrea,

So, I'm getting the user that way, I'm in doubt is how to mount the routes 
to an account view, where you have the retrieve, update and delete of the 
current user.

Em sexta-feira, 27 de julho de 2018 12:23:24 UTC-3, Andréas Kühne escreveu:
>
> Hi Fernando,
>
> In DRF even with token authentication you will be able to get the 
> currently logged in user via the user object on the request. So 
> request.user will be the user doing the request.
>
> If you for example want to have an endpoint that is for the current user 
> you could just check the request.user to see which user is doing the 
> request. Then you don't need to use the id from the url.
>
> Regards,
>
> Andréas
>
> 2018-07-27 16:36 GMT+02:00 Fernando Miranda  >:
>
>> I think I understood about the rest, the right one to edit for example 
>> would be to have the route of type PUT passing the token OAuth2 in the 
>> route and there I look for the user owner of the token? Or the user ID 
>> and check if the authenticated user is the same as the last ID?
>>
>> Em sexta-feira, 27 de julho de 2018 08:10:19 UTC-3, Jason escreveu:
>>>
>>> you can probably do this with overriding a few things, but for me, your 
>>> use case has some major problems.  you're effectively breaking away from 
>>> the basics of REST.
>>>
>>> If you want to implement some sort of non-sequential identifiers for 
>>> users/resources, use UUIDs.  Any token passed in the headers should be used 
>>> for auth only, not contain explicit routing values.
>>>
>>> On Thursday, July 26, 2018 at 4:10:55 PM UTC-4, Fernando Miranda wrote:

 Hello, I'm using Django Rest Framework, I was wondering if you have how 
 to change the default url of an endpoint in a view? In case it is a 
 view of account where I wanted the retrieve method to be without / {id} 
 this also for the delete and edit because I will identify the user by the 
 token passed in the header.

>>> -- 
>> 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 post to this group, send email to django...@googlegroups.com 
>> .
>> Visit this group at https://groups.google.com/group/django-users.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-users/875eb467-9cd2-492b-9aea-e311bb3da022%40googlegroups.com
>>  
>> 
>> .
>>
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/554e0c24-29d7-474a-982b-cb40838b2b14%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Optimizing Prefetch for Postgres IN Limit

2018-07-27 Thread Simon Charette
There's two open tickets to work around this issue.

https://code.djangoproject.com/ticket/25464 which allows passing queryset 
override to be used for retrieval and another one that I can't currently 
find that allows specifying that a subquery should be used instead of a an 
IN clause.

Simon


Le vendredi 27 juillet 2018 07:05:17 UTC-4, Jason a écrit :
>
> well, prefetch explicitly does joining in python, as in the docs
>
>
> https://docs.djangoproject.com/en/2.0/ref/models/querysets/#django.db.models.query.QuerySet.prefetch_related
>
> prefetch_related, on the other hand, does a separate lookup for each 
> relationship, and does the ‘joining’ in Python.
>
> since it doesn't use joins, there's nothing really to generate a match on 
> an index.  FWIW, I'm facing a similar issue when optimizing a slow django 
> query.
>
>
> On Friday, July 27, 2018 at 1:10:35 AM UTC-4, Ram Jayaraman wrote:
>>
>> Hi Xof,
>>
>> The issue is that, when you do a naive Prefetch you are left with nothing 
>> but Django’s auto generated IN query. I was asking about he recommended way 
>> to make this into a JOIN, which is not obvious how to do with Django
>>
>> Also in my case the naive IN query on the PK *did not* generate a Index 
>> scan and ended up being a full table scan
>>
>> I could have however used a Prefetch object to force PG to use a 
>> different index apart from the PK but a naive Prefetch of the form 
>> queryset.prefetch(‘table__table2’) does not use any index beyond 100 values
>>
>> Also it’s hard to estimate how many values will be there in a nested 
>>  prefetch, to state the obvious, to do anything different on a query by 
>> query basis
>>
>>
>> On Thu, Jul 26, 2018 at 19:35 Christophe Pettus  
>> wrote:
>>
>>>
>>> > On Jul 25, 2018, at 02:59, Jason  wrote:
>>> > 
>>> > Where do you get that in the pg documentation? I can't find that 
>>> anywhere (google-fu may be failing me), and we do have some queries with 
>>> more than 100 values using IN.
>>>
>>> It's slightly more complicated than that.  Above 100 entries, the 
>>> PostgreSQL optimizer won't try to do optimizations of the form changing i 
>>> IN (1, 2 ..., 101) to (i = 1) OR (i = 2) OR  (i = 3)... to see if there's a 
>>> better way of executing the query.  It *can* still do an index scan in 
>>> those cases, although the more entries in the IN list, the less efficient 
>>> that will be.  In general, large IN clauses aren't a great idea; they're 
>>> better replaced with a join.
>>>
>>> --
>>> -- Christophe Pettus
>>>x...@thebuild.com
>>>
>>> -- 
>>> You received this message because you are subscribed to a topic in the 
>>> Google Groups "Django users" group.
>>> To unsubscribe from this topic, visit 
>>> https://groups.google.com/d/topic/django-users/y9sVr9Pbr-o/unsubscribe.
>>> To unsubscribe from this group and all its topics, send an email to 
>>> django-users...@googlegroups.com.
>>> To post to this group, send email to django...@googlegroups.com.
>>> Visit this group at https://groups.google.com/group/django-users.
>>> To view this discussion on the web visit 
>>> https://groups.google.com/d/msgid/django-users/8A21227D-94A9-459F-89D0-771D3052C0C0%40thebuild.com
>>> .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>

-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/231b4aba-3b79-4c39-985d-08399c2d5a7a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Change the default route of a view

2018-07-27 Thread Andréas Kühne
Hi Fernando,

In DRF even with token authentication you will be able to get the currently
logged in user via the user object on the request. So request.user will be
the user doing the request.

If you for example want to have an endpoint that is for the current user
you could just check the request.user to see which user is doing the
request. Then you don't need to use the id from the url.

Regards,

Andréas

2018-07-27 16:36 GMT+02:00 Fernando Miranda :

> I think I understood about the rest, the right one to edit for example
> would be to have the route of type PUT passing the token OAuth2 in the
> route and there I look for the user owner of the token? Or the user ID
> and check if the authenticated user is the same as the last ID?
>
> Em sexta-feira, 27 de julho de 2018 08:10:19 UTC-3, Jason escreveu:
>>
>> you can probably do this with overriding a few things, but for me, your
>> use case has some major problems.  you're effectively breaking away from
>> the basics of REST.
>>
>> If you want to implement some sort of non-sequential identifiers for
>> users/resources, use UUIDs.  Any token passed in the headers should be used
>> for auth only, not contain explicit routing values.
>>
>> On Thursday, July 26, 2018 at 4:10:55 PM UTC-4, Fernando Miranda wrote:
>>>
>>> Hello, I'm using Django Rest Framework, I was wondering if you have how
>>> to change the default url of an endpoint in a view? In case it is a
>>> view of account where I wanted the retrieve method to be without / {id}
>>> this also for the delete and edit because I will identify the user by the
>>> token passed in the header.
>>>
>> --
> 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 post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/875eb467-9cd2-492b-9aea-e311bb3da022%40googlegroups.com
> 
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAK4qSCdBiFuMgVtj%3DSdK0nPvBru353HGEGwDr6kfKH0%3DZh%2BgbQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Change the default route of a view

2018-07-27 Thread Fernando Miranda
I think I understood about the rest, the right one to edit for example 
would be to have the route of type PUT passing the token OAuth2 in the 
route and there I look for the user owner of the token? Or the user ID and 
check if the authenticated user is the same as the last ID?

Em sexta-feira, 27 de julho de 2018 08:10:19 UTC-3, Jason escreveu:
>
> you can probably do this with overriding a few things, but for me, your 
> use case has some major problems.  you're effectively breaking away from 
> the basics of REST.
>
> If you want to implement some sort of non-sequential identifiers for 
> users/resources, use UUIDs.  Any token passed in the headers should be used 
> for auth only, not contain explicit routing values.
>
> On Thursday, July 26, 2018 at 4:10:55 PM UTC-4, Fernando Miranda wrote:
>>
>> Hello, I'm using Django Rest Framework, I was wondering if you have how 
>> to change the default url of an endpoint in a view? In case it is a view 
>> of account where I wanted the retrieve method to be without / {id} this 
>> also for the delete and edit because I will identify the user by the token 
>> passed in the header.
>>
>

-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/875eb467-9cd2-492b-9aea-e311bb3da022%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: avoiding import-time queries

2018-07-27 Thread clavierplayer
I've been experimenting with it and it seems to be working pretty well. 
Thank you, Julio!

On Thursday, July 26, 2018 at 2:28:18 PM UTC-4, Julio Biason wrote:
>
> Hi Clarvierplayer,
>
> Dunno if that's a best practice, but I'd add a module in the same app with 
> functions to retrieve the information and use cache (
> https://docs.djangoproject.com/en/2.0/topics/cache/#basic-usage) 
> copiously. So, instead of loading the values at start up time, the values 
> would be filled only after the first time it was requested.
>
> On Thu, Jul 26, 2018 at 3:21 PM, > 
> wrote:
>
>> I'm looking for some best-practice (or at least workable) suggestions for 
>> avoiding import-time queries. I have some database records that are 
>> constant, and these constants are used all over the application. Is there 
>> any way to get those constants out of the database once at startup? 
>>
>> At present, I am working on a set of apps with a single underlying 
>> database. And they wouldn't build at all when I tried to migrate a new test 
>> database because of import-time queries, because it wants to validate the 
>> references to those constants at build time (and can't, because in this 
>> case my new test database has no tables yet). Is there any way to store 
>> these model records in something that will persist between builds (kind of 
>> like test fixtures)? A StackOverflow discussion 
>> 
>>  suggested 
>> the Python Lazy Object Proxy, but that project appears to have been 
>> abandoned. The Django docs themselves simply recommend avoiding 
>> query-at-import behaviors--I can definitely see it causes all kinds of 
>> problems that are not easy to circumvent--but they make no recommendations 
>> for alternatives. Is it indeed necessary to keep querying for these all 
>> over the app? Is there a third-party solution that I've overlooked?
>>
>> Django 2.0. Thank you!
>>
>> -- 
>> 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 post to this group, send email to django...@googlegroups.com 
>> .
>> Visit this group at https://groups.google.com/group/django-users.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-users/9510552d-9afe-47b0-842e-6f80e9a28bbe%40googlegroups.com
>>  
>> 
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
>
> -- 
> *Julio Biason*, Sofware Engineer
> *AZION*  |  Deliver. Accelerate. Protect.
> Office: +55 51 3083 8101  |  Mobile: +55 51 *99907 0554*
>

-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/b487bf9d-359c-4541-82a9-2b6228fe9437%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: learn dijango

2018-07-27 Thread Rajdeep Bharati
Hi Arul,
You should be comfortable with the basics of Python and HTML to get started 
with Django.
Head over to: https://docs.python.org/3/tutorial/ and follow that tutorial 
for python, also practice coding in a text editor and the python 
interpreter.
After you are through with that, then you can start with the Django polls 
app tutorial: https://docs.djangoproject.com/en/2.0/intro/tutorial01/

Regards,
Rajdeep

On Thursday, July 26, 2018 at 7:44:45 PM UTC+5:30, arul wrote:
>
> *I just entered the programming world and i know little bit 
> javascript,HTML can learn even I don't  know any oop/scripting language can 
> I learn django for now is there any possibility*
>
>

-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/a6fed9d1-5582-49a6-a79e-4fa23e846708%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Change the default route of a view

2018-07-27 Thread Jason
you can probably do this with overriding a few things, but for me, your use 
case has some major problems.  you're effectively breaking away from the 
basics of REST.

If you want to implement some sort of non-sequential identifiers for 
users/resources, use UUIDs.  Any token passed in the headers should be used 
for auth only, not contain explicit routing values.

On Thursday, July 26, 2018 at 4:10:55 PM UTC-4, Fernando Miranda wrote:
>
> Hello, I'm using Django Rest Framework, I was wondering if you have how to 
> change the default url of an endpoint in a view? In case it is a view of 
> account where I wanted the retrieve method to be without / {id} this also 
> for the delete and edit because I will identify the user by the token 
> passed in the header.
>

-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/1d9422ca-5c6f-4d4d-8fa3-5977af241f30%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Optimizing Prefetch for Postgres IN Limit

2018-07-27 Thread Jason
well, prefetch explicitly does joining in python, as in the docs

https://docs.djangoproject.com/en/2.0/ref/models/querysets/#django.db.models.query.QuerySet.prefetch_related

prefetch_related, on the other hand, does a separate lookup for each 
relationship, and does the ‘joining’ in Python.

since it doesn't use joins, there's nothing really to generate a match on 
an index.  FWIW, I'm facing a similar issue when optimizing a slow django 
query.


On Friday, July 27, 2018 at 1:10:35 AM UTC-4, Ram Jayaraman wrote:
>
> Hi Xof,
>
> The issue is that, when you do a naive Prefetch you are left with nothing 
> but Django’s auto generated IN query. I was asking about he recommended way 
> to make this into a JOIN, which is not obvious how to do with Django
>
> Also in my case the naive IN query on the PK *did not* generate a Index 
> scan and ended up being a full table scan
>
> I could have however used a Prefetch object to force PG to use a different 
> index apart from the PK but a naive Prefetch of the form 
> queryset.prefetch(‘table__table2’) does not use any index beyond 100 values
>
> Also it’s hard to estimate how many values will be there in a nested 
>  prefetch, to state the obvious, to do anything different on a query by 
> query basis
>
>
> On Thu, Jul 26, 2018 at 19:35 Christophe Pettus  > wrote:
>
>>
>> > On Jul 25, 2018, at 02:59, Jason > 
>> wrote:
>> > 
>> > Where do you get that in the pg documentation? I can't find that 
>> anywhere (google-fu may be failing me), and we do have some queries with 
>> more than 100 values using IN.
>>
>> It's slightly more complicated than that.  Above 100 entries, the 
>> PostgreSQL optimizer won't try to do optimizations of the form changing i 
>> IN (1, 2 ..., 101) to (i = 1) OR (i = 2) OR  (i = 3)... to see if there's a 
>> better way of executing the query.  It *can* still do an index scan in 
>> those cases, although the more entries in the IN list, the less efficient 
>> that will be.  In general, large IN clauses aren't a great idea; they're 
>> better replaced with a join.
>>
>> --
>> -- Christophe Pettus
>>x...@thebuild.com 
>>
>> -- 
>> You received this message because you are subscribed to a topic in the 
>> Google Groups "Django users" group.
>> To unsubscribe from this topic, visit 
>> https://groups.google.com/d/topic/django-users/y9sVr9Pbr-o/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to 
>> django-users...@googlegroups.com .
>> To post to this group, send email to django...@googlegroups.com 
>> .
>> Visit this group at https://groups.google.com/group/django-users.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-users/8A21227D-94A9-459F-89D0-771D3052C0C0%40thebuild.com
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>

-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/8ddc73c4-d670-4482-88f7-badaa1346b69%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Question] Django 2 - how to sum values based on filter and group by Year-Month

2018-07-27 Thread Melvyn Sopacua
On vrijdag 27 juli 2018 04:25:56 CEST Charles Sartori wrote:

> I need to Sum() the values filtering it with
> 1 - sum(values) where date < first day of the month
> 2 - sum(values) where date < last day of the month
> 
> Expected result(something like that):
> [
>  {'year-month': '2018-01'}, {'sum_before_month_day_one': *0*},
> {'sum_before_last_month_day': *60*},
>  {'year-month': '2018-02'}, {'sum_before_month_day_one': *60*},
> {'sum_before_last_month_day': *70*},
>  {'year-month': '2018-03'}, {'sum_before_month_day_one': *70*},
> {'sum_before_last_month_day': *100*},
> ]

Break it down:
- the only relevant part is the sum of the month
- the other is a running total you can keep track of without putting the 
burdon on the database

But, requirement 2) applied to the first result should strictly yield 30, not 
60. But I think that's an error in your requirement description.

> {'date': datetime.date(2018, 1, 1), 'quantity': 60.0}

(30 is *at* the last day of the month, not before it)
> 2018-01-31  30

-- 
Melvyn Sopacua


-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/2165075.IrKYnSaKcf%40fritzbook.
For more options, visit https://groups.google.com/d/optout.


Re: [Question] Django 2 - how to sum values based on filter and group by Year-Month

2018-07-27 Thread Gerald Brown
I am having a similar problem where I want to get the total (SUM) of 
about 20 - 30 items in a current day.  I can get the list of customers 
names and payments in a html table but so far haven't been able to get 
the SUM of those payments.  I have tried using Django ORM AGGREGATE 
SUM('integer field') but I am not able to get the results.


I haven't tried grouping yet in my project but will probably have to in 
the near future.  I think it is possible to group by week & month and 
then get a sum of those groups.  MariaDB has a function "MONTH" and " 
WEEKOFYEAR" which I hope will help with the grouping.  One problem that 
I have is that in my customer table I have a ForeignKey link to a 
Payment_amount table so the user can look up the payment amount instead 
of typing it in where an extra/less zero could be entered.


One web site that might offer some suggestions on solving your/my 
problem is: 
https://simpleisbetterthancomplex.com/tutorial/2016/12/06/how-to-create-group-by-queries.html. 
This site has a lot of posts relating to Django.


Good luck & I would be interested in how you solved this problem.

Gerald S. Brown

On Friday, 27 July, 2018 10:25 AM, Charles Sartori wrote:

I have the following table:

2018-01-01      10
2018-01-15      20
2018-01-31      30
2018-02-01      10
2018-03-01      10
2018-03-20      20

I need to Sum() the values filtering it with
1 - sum(values) where date < first day of the month
2 - sum(values) where date < last day of the month

Expected result(something like that):
[
 {'year-month': '2018-01'}, {'sum_before_month_day_one': *0*}, 
{'sum_before_last_month_day': *60*},
 {'year-month': '2018-02'}, {'sum_before_month_day_one': *60*}, 
{'sum_before_last_month_day': *70*},
 {'year-month': '2018-03'}, {'sum_before_month_day_one': *70*}, 
{'sum_before_last_month_day': *100*},

]

So far I manage to sum values and group by month:

In [12]: result = Sale.objects \
    ...:     .annotate(date=TruncMonth('event_date')) \
    ...:     .values('date') \
    ...:     .annotate(quantity=Sum('quantity')) \
    ...:     .values('date', 'quantity') \
    ...:     .order_by('date')

In [13]: for i in result: print(i)

{'date': datetime.date(2018, 1, 1), 'quantity': 60.0}
{'date': datetime.date(2018, 2, 1), 'quantity': 10.0}
{'date': datetime.date(2018, 3, 1), 'quantity': 30.0}
--
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 post to this group, send email to django-users@googlegroups.com 
.

Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/a57a7058-ea08-4909-8e98-c678aa9969cc%40googlegroups.com 
.

For more options, visit https://groups.google.com/d/optout.


--
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/ca3a45c8-7fec-fc94-1e90-d95e3726462d%40gmail.com.
For more options, visit https://groups.google.com/d/optout.