I'm stuck on a query I'm trying to execute. I have attached an image of the datamodel below. Essentally, I got 6 tables with various relations. Here's a quick summary:
One User can have many Projects, and each Expense is tied to a specific project
Each project can have many users, and each user can have many projects, put together through a joined table - UserProject
To indicate the level of access a user has to a specific project, a field called role is added to the UserProject table - a user can either be a member or an admin of a project
The query I wish to construct is to fetch all expenses that are created by the logged in user (request.user) and all expenses of all projects where the user has the role of admin from the UserProject table.
See image of data model below:
Any idea how I would proceed with that query?
I think I figured it out by using Q queries
specific_project_users = Expense.objects.filter(
Q(project__projectuser__user=self.user),
Q(project__projectuser__role='admin') | Q(user=self.user)
).values("user__id")
users = User.objects.filter(id__in=specific_project_users)
Not sure how efficient the query is though.
The raw SQL query translates to this
SELECT ...
FROM "accounts_user"
WHERE "accounts_user"."id"
IN (SELECT U0."user_id"
FROM "finances_expense" U0
INNER JOIN "project" U1
ON (U0."project_id" = U1."id")
INNER JOIN "projectuser" U2 ON (U1."id" = U2."project_id")
WHERE (U2."user_id" = 1 AND (U2."role" = admin OR U0."user_id" = 1)))
Related
I have a column named company ID that contains various comapnies with differnet employes in them, I need a DAX Query which will give data A/Q to the Company id, suppose if there are 3 employes inside a company and each of them have a companyid 1 then I they should able to see the reports of each other but they cannot be able to see the reports of comanyid 2 and 3, how this can be achived?
I know I can do this by creating different Roles for each companyid, but how can this be achived if I want this to be built in one particular role???
What you are asking is to implement Dynamic Row Level Security.
Model:
User Table: Table that contains user detail along with the field on which we will apply security(here email field).
Company Table: Table containing company data .
User Company Bridge: Bridge table that contains permission details, for example user x is member of company y and z.
Company Data Table: Measures or transaction information of company that is to be filtered.
Defining RLS(Row Level Security):
In Modelling -> Manage Roles, create a new role on Email of User Table by this DAX query which returns the email id of logged in user.
[Email] = userprincipalname()
Finalizing:
Go to PowerBI Service -> Dataset -> Security and add users to the roles created.
To test the implementation:
Go to Modelling tab of pbix file.
Click on View As Roles.
Check other User checkbox and put an email ID and also check Profile
checkbox.
Now you can see data filtered.
In this manner it becomes easy to maintain roles and security by just modifying the bridge table that stores all permission details.
look at the following scenario:
I have an User model and an Address model that belongs to user.
In the user index, I need to show along with user's info how many addresses does the user have, but it's generating N+1 queries as everytime I call count it executes an additional query for that user id.
How can I do that? I read about select_related but I'm trying to make it in the reverse order...
In SQL it could be translated to:
SELECT user.*,
(SELECT count(*) FROM address WHERE address.user_id = user.id) AS address_count
FROM user
Is there a way to get the above SQL with django QuerySet?
You can annotate the number of addresses, you haven't shown your models but you can use the following on your queryset
.annotate(address_count=Count('address'))
User.objects.all().annotate(address_count=Count('address')) # Im guessing you want this
This would provide an address_count property on for each result
Docs for count
I have an existing table called empname in my postgres database
(Projectid,empid,name,Location) as
(1,101,Raj,India),
(2,201,David,USA)
So in the app console it will have like the following
1)Projectid=Textbox
2)Ops =(view,insert,Edit)-Dropdown
Case1:
So if i write project id as 1 and select View Result:It will display all the records for Projectid =1(Here 1 record)
Case2:
If i write projectid as 3 and select insert it will ask for all the inputs like empid,name,address and based on that it will update the table .
Case3:
If i write projectid as 2 and select edit.Then it will show all the field for that id and user can edit any column and can save which will update the records in backend for the existing table
If there is not data found for the respective project id then it will display no records found
Please help me on this as I am stuck up with models
Once you have your models created, the next task should be the form models. I can identify atleast 3 form classes that you will need to create. One to display the information(case 1), another to collect information(case 2) and the last class to edit the information. Wire up the form to the views and add the urls.
A good reference could be a django a user registration form since it will have all the three cases taken care of.http://www.tangowithdjango.com/book17/chapters/login.html
I've got 2 existing models that I need to join that are non-relational (no foreign keys). These were written by other developers are cannot be modified by me.
Here's a quick description of them:
Model Process
Field filename
Field path
Field somethingelse
Field bar
Model Service
Field filename
Field path
Field servicename
Field foo
I need to join all instances of these two models on the filename and path columns. I've got existing filters I have to apply to each of them before this join occurs.
Example:
A = Process.objects.filter(somethingelse=231)
B = Service.objects.filter(foo='abc')
result = A.filter(filename=B.filename,path=B.path)
This sucks, but your best bet is to iterate all models of one type, and issue queries to get your joined models for the other type.
The other alternative is to run a raw SQL query to perform these joins, and retrieve the IDs for each model object, and then retrieve each joined pair based on that. More efficient at run time, but it will need to be manually maintained if your schema evolves.
A relevant image of my model is here: http://i.stack.imgur.com/xzsVU.png
I need to make a queryset that contains all cats who have an associated person with a role of "owner" and a name of "bob".
The sql for this would be shown below.
select * from cat where exists
(select 1 from person inner join role where
person.name="bob" and role.name="owner");
This problem can be solved in two sql queries with the following django filters.
people = Person.objects.filter(name="bob", role__name="owner")
ids = [p.id for p in people]
cats = Cat.objects.filter(id__in=ids)
My actual setup is more complex than this and is dealing with a large dataset. Is there a way to do this with one query? If it is impossible, what is the efficient alternative?
I'm pretty sure this is your query:
cats = Cat.objects.filter(person__name='bob', person__role__name='owner')
read here about look ups spanning relationships