Adding Objects to Query Set

52 views
Skip to first unread message

Vibhu Rishi

unread,
Dec 16, 2013, 8:47:13 AM12/16/13
to django...@googlegroups.com
Hi

I am not able to figure this out. I want to add objects to a query set.

I have 2 models :
1. Projects which is basically a list of projects. I have a field which defines the owner of the project. e.g. :

class Project(models.Model):
    ...
    owner = models.ForeignKey(User)

2. ProjectMember - This is a table where I add any user ( other than owner ) if they are member of the project.

class ProjectMember(models.Model):
    project = models.ForeignKey(Project)
    member = models.ForeignKey(User)
    added_on = models.DateTimeField()

What I am trying to do is get a list of projects which the current user is either owner or member of. So, I have this in my view :

def mylist(request):
    projects = Project.objects.filter(owner=request.user)
    member_of = ProjectMember.objects.filter(member = request.user)
    # Now find all projects the user is a member of and add to the projects list
    all_projects = projects
    for m in member_of:
        all_projects |= m.project
    return render (request, "project/projects_mine.html", {'projects':projects})
 

I am doing something wrong here as the line all_projects |= m.project is not working. I tried with a += also.

How can I achieve what I am trying to do ?

Vibhu

--
Simplicity is the ultimate sophistication. - Leonardo da Vinci
Life is really simple, but we insist on making it complicated. - Confucius

Sebastian Morkisch

unread,
Dec 16, 2013, 9:51:00 AM12/16/13
to django...@googlegroups.com
Hi

I am working on the same thing. Same here, one project model, one extended User model. Well, when I printed the request, I got the Usename, which is not an id, but you'd like to compare ids. Is that correct?

So perhaps you're closer to a solution, if you have a method like this:

def get_queryset(self):
   return Project.objects.filter(owner=self.request.user.id) <-- see that id?

Best regards,
Sebastian

Andrew Farrell

unread,
Dec 16, 2013, 9:51:01 AM12/16/13
to django...@googlegroups.com
It sounds like you just want a set of projects and don't actually need a querySet.
You can do this with 
projects = set( Project.objects.filter(owner=request.user).all() )
member_projects = set( ProjectMember.objects.filter(member = request.user) )
total_projects = set(projects) | set(member_projects)

You could also do 
projects = Project.objects.filter(owner=request.user).all()
member_projects = ProjectMember.objects.filter(member = request.user).exclude(project__owner=request.user).all()
total_projects = projects + member_projects


--
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPiONwm9%2Bnp_BROqjVEfmDaE_KgAnYnhv8u-GuiFBG7q2QeuaA%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

Vibhu Rishi

unread,
Dec 16, 2013, 11:40:31 AM12/16/13
to django...@googlegroups.com
Hi Sebastian,

Not really. I want the list of project objects which is coming ok by filtering on the user object. I checked it from commandline, and I am getting it correctly.

The problem is the 2nd table. I want to get the projects from the 1st table when the user is a member as specified in the 2nd table.

e.g. following is the full list of projects. + denotes that I am owner. * denotes where I am a member.

Project1
Project2 ( + )
Project 3
Project4 ( * )

When I do a projects = Project.objects.filter(owner=request.user) I get a query set like :
[<Project:Project1>]

Now, when I do member_projects= Member.objects.filter(member=request.user) I get a query set like:
[<ProjectMember: Project4>]

So far so good. I dont have any issues from them. However, in my templates, now I need to pass on 2 querysets because these objects are different. I have 2 loops for listing the projects. one for projects where I print the name as  {{ o.name }} and one for the ProjectMember where I print the name as o.project.name . I want to make the code neater and have just one loop :)

Now from the 2nd queryset ( since the object type is different), I want to take out the actual project objects and add to the 1st set. Thats where I am hitting a roadblock.

for o in member_projects
   o.project

works fine, as I get the project object.

However, now I want to add this project into the 1st queryset  - and there i am not able to.

Hope this clears what I am trying to achieve!

V. 

--
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 http://groups.google.com/group/django-users.

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

Vibhu Rishi

unread,
Dec 16, 2013, 11:46:08 AM12/16/13
to django...@googlegroups.com
Hi Andrew,

The set seem to add on properly. How do i now access the project detail ? When I try to iterate over it since this set is made of 2 different object types, it fails for one of them.

e.g. i have in the project model attribute as 'name' for the project. so when i iterate over the list like :

for o in totoal_projects:
  print o.name

this fails when it hits the ProjectMember objects as for them the name is actually o.project.name

V.



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

antialiasis

unread,
Dec 16, 2013, 12:56:27 PM12/16/13
to django...@googlegroups.com
Rather than fetching ProjectMember.objects.filter(member=request.user), you want to fetch Project.objects.filter(projectmember_set__member=request.user). That will get you Project objects rather than ProjectMember objects.

Arnold Krille

unread,
Dec 17, 2013, 3:27:10 AM12/17/13
to django...@googlegroups.com
Hi,

define related names in ProjectMember:

class ProjectMember(models.Model):
project = models.ForeignKey(Project, related_name='members')
member = models.ForeignKey(User)
added_on = models.DateTimeField()

The full query to get all Projects the User is either member or leader:

Projects.objects.filter(
Q(owner=user) | Q(members__member=user)
)

Don't merge in python what you can merge in the database. One
query in the database is (almost) always faster then two queries and
merging in python.

Have fun,

Arnold

Am Mon, 16 Dec 2013 09:56:27 -0800 (PST)
schrieb antialiasis <antia...@gmail.com>:
signature.asc

Vibhu Rishi

unread,
Dec 17, 2013, 7:19:56 AM12/17/13
to django...@googlegroups.com
This seems to be working with a slite modification. the _set was giving an error. So, I changed to :

Project.objects.filter(projectmember__member=request.user)

I am not sure if that will just return 1 result or all the results. I have to check that out.

Vibhu


--
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 http://groups.google.com/group/django-users.

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

Vibhu Rishi

unread,
Dec 17, 2013, 7:23:57 AM12/17/13
to django...@googlegroups.com
I will try this out also. Thanks!
Reply all
Reply to author
Forward
0 new messages