How to include a Where clause for each query on django?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How to include a Where clause for each query on django?

fellipeh
Hello..


Each model of my app, has a field named  user... so, I need to filter any query on that model user=current_user...

I already do that, but manually for each model, on a view.. and I like to do that directly on some where when I try to use a query, any record will be filtered.....


Can I do that? where is the best place to do that?

Thanks! 


--
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 [hidden email].
To post to this group, send email to [hidden email].
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/CAF1jwZFc_dSyWQSDXJ-qaRgCvzoPYwK%2BRowd91aGCj8rnEnu1w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

RE: How to include a Where clause for each query on django?

Matthew Pava

Just create a custom manager and pass in the current user to it.

https://docs.djangoproject.com/en/2.0/topics/db/managers/

 

class MyCustomManager(models.Manager):

def with_current_user(current_user):

                                return self.filter(user=current_user)

 

In y our model:

 

class MyModel:

                objects = MyCustomerManager()

 

From: [hidden email] [mailto:[hidden email]] On Behalf Of Fellipe Henrique
Sent: Thursday, June 14, 2018 9:09 AM
To: Django Users
Subject: How to include a Where clause for each query on django?

 

Hello..

 

 

Each model of my app, has a field named  user... so, I need to filter any query on that model user=current_user...

 

I already do that, but manually for each model, on a view.. and I like to do that directly on some where when I try to use a query, any record will be filtered.....

 

 

Can I do that? where is the best place to do that?

 

Thanks! 

 

 

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/CAF1jwZFc_dSyWQSDXJ-qaRgCvzoPYwK%2BRowd91aGCj8rnEnu1w%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 [hidden email].
To post to this group, send email to [hidden email].
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/d01746b20cb0454d8bf550c33b1a88ad%40ISS1.ISS.LOCAL.
For more options, visit https://groups.google.com/d/optout.