How to filter *every* query on django?

55 views
Skip to first unread message

Fellipe Henrique

unread,
Aug 17, 2018, 2:22:32 PM8/17/18
to Django Users
Hello guys... I have several model with FK with Django User.... so, I need to filter each query using:

.filter(created_user=current)user)

I do it, manually... how can do it, automatically?


Thanks


T.·.F.·.A.·.     S+F
Fellipe Henrique P. Soares

e-mail: > echo "lkrrovknFmsgor4ius" | perl -pe \ 's/(.)/chr(ord($1)-2*3)/ge'
Twitter: @fh_bash
Message has been deleted

Rohit Rana

unread,
Aug 17, 2018, 5:54:38 PM8/17/18
to django...@googlegroups.com
I have created models and when iam trying to acces these models it is giving me this error. please help me out

On Sat, Aug 18, 2018, 12:25 AM Ángel Gabriel Morales Acosta <angel.ga...@gmail.com> wrote:
Hi Bro.·.

If I understand you, you can this:

def any_func(..., request):
...
     .filter(created_user=request.user)


Cheers Bro.·.


---
Ángel Gabriel Morales Acosta


--
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/CAF1jwZEtTYiD%3DTvPxuvucMRV%2BeLzCD%3DXMWdMSKt2sV2fbxYeMA%40mail.gmail.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...@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/CAPF44MAtRpPmNgF%2BpDHbcemRHP30tDtCgp-S0Hoae%3DAdJw_7xQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
IMG_20180818_025521.jpg
Message has been deleted

Fellipe Henrique

unread,
Aug 18, 2018, 7:50:32 AM8/18/18
to Django Users
Hi Bro .`.

Thanks for your response, but I need to do that:

If I make these:

Products.object.all()
or
Products.objects.count()

I need to django, auto include a .filter(created_user=request.user) ... today I`m doing exactly you write, but I need to make these filter, one-by-one...



T.·.F.·.A.·.     S+F
Fellipe Henrique P. Soares

e-mail: > echo "lkrrovknFmsgor4ius" | perl -pe \ 's/(.)/chr(ord($1)-2*3)/ge'
Twitter: @fh_bash


On Fri, Aug 17, 2018 at 3:55 PM Ángel Gabriel Morales Acosta <angel.ga...@gmail.com> wrote:
Hi Bro.·.

If I understand you, you can this:

def any_func(..., request):
...
     .filter(created_user=request.user)


Cheers Bro.·.


---
Ángel Gabriel Morales Acosta


On Fri, Aug 17, 2018 at 1:22 PM Fellipe Henrique <fell...@gmail.com> wrote:
--
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/CAF1jwZEtTYiD%3DTvPxuvucMRV%2BeLzCD%3DXMWdMSKt2sV2fbxYeMA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Jason

unread,
Aug 18, 2018, 8:10:31 AM8/18/18
to Django users

Fellipe Henrique

unread,
Aug 18, 2018, 11:46:50 AM8/18/18
to Django Users
Thanks Jason, 

But, can I get current user on model manager? I need to filter with that info.



T.·.F.·.A.·.     S+F
Fellipe Henrique P. Soares

e-mail: > echo "lkrrovknFmsgor4ius" | perl -pe \ 's/(.)/chr(ord($1)-2*3)/ge'
Twitter: @fh_bash

On Sat, Aug 18, 2018 at 9:10 AM Jason <jjohn...@gmail.com> wrote:
--
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.
Message has been deleted

Andréas Kühne

unread,
Aug 19, 2018, 4:14:13 AM8/19/18
to django...@googlegroups.com
What you can do is add a middleware that sets the current user in a thread local variable. That way you can then add a custom model manager that reads that variable.

Then all of your filters will be set automatically.

Regards,

Andréas

2018-08-18 17:46 GMT+02:00 Fellipe Henrique <fell...@gmail.com>:
Thanks Jason, 

But, can I get current user on model manager? I need to filter with that info.



T.·.F.·.A.·.     S+F
Fellipe Henrique P. Soares

e-mail: > echo "lkrrovknFmsgor4ius" | perl -pe \ 's/(.)/chr(ord($1)-2*3)/ge'
Twitter: @fh_bash

On Sat, Aug 18, 2018 at 9:10 AM Jason <jjohn...@gmail.com> wrote:

--
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+unsubscribe@googlegroups.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+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Fellipe Henrique

unread,
Aug 19, 2018, 10:01:40 AM8/19/18
to Django Users
Thanks Andreas,

Using your tip, I installed django-tools app, and now I can get the current user junt using single line... :)

One question... these locals variable, change for each connection?  example:  I have 1 site, and I want to get several models, with FK for each user.. so.. User1 can only see data from him.. that's the reason I need to filter...  Works like these?

T.·.F.·.A.·.     S+F
Fellipe Henrique P. Soares

e-mail: > echo "lkrrovknFmsgor4ius" | perl -pe \ 's/(.)/chr(ord($1)-2*3)/ge'
Twitter: @fh_bash

On Sun, Aug 19, 2018 at 5:14 AM Andréas Kühne <andrea...@hypercode.se> wrote:
What you can do is add a middleware that sets the current user in a thread local variable. That way you can then add a custom model manager that reads that variable.

Then all of your filters will be set automatically.

Regards,

Andréas

2018-08-18 17:46 GMT+02:00 Fellipe Henrique <fell...@gmail.com>:
Thanks Jason, 

But, can I get current user on model manager? I need to filter with that info.



T.·.F.·.A.·.     S+F
Fellipe Henrique P. Soares

e-mail: > echo "lkrrovknFmsgor4ius" | perl -pe \ 's/(.)/chr(ord($1)-2*3)/ge'
Twitter: @fh_bash

On Sat, Aug 18, 2018 at 9:10 AM Jason <jjohn...@gmail.com> wrote:

--
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.

--
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.

--
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.

Andréas Kühne

unread,
Aug 20, 2018, 4:41:47 AM8/20/18
to django...@googlegroups.com
I don't know how django-tools solves it - but it usually is set for every request to the server.

When running django you have 1 or more threads that run the server. Each thread can handle one request at a time - others get queued (you don't usually see this). So when the request is done the middleware should clear the user (that's how I wrote it at least - it's perhaps overkill, but better safe than sorry). The next incoming request will then have a new user - even if it is the same user from the previous one.

Regards,

Andréas


Regards,

Andréas

To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.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+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

--
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+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

--
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+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages