Monday, December 16, 2013

Re: Adding Objects to Query Set

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.


On Mon, Dec 16, 2013 at 8:21 PM, Andrew Farrell <armorsmith42@gmail.com> wrote:
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


On Mon, Dec 16, 2013 at 8:47 AM, Vibhu Rishi <vibhu.rishi@gmail.com> wrote:
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

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

--
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-users@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/CA%2By5TLaNBRwaYuRmJq1hzF2Lfb67dnOduu3%3D46_7TDX0TtRx%2Bw%40mail.gmail.com.

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



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

--
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-users@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/CAPiONwkpAYipfg1n8MtwqLSjRuYKVG_cYmNArsC87o_3sUV_wQ%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate