Tuesday, December 17, 2013

Re: Adding Objects to Query Set

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


On Mon, Dec 16, 2013 at 11:26 PM, antialiasis <antialiasis@gmail.com> wrote:
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.


On Monday, December 16, 2013 1:47:13 PM UTC, Vibhu Rishi 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/094848da-0d22-47a2-b97a-0abed22cc5e2%40googlegroups.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/CAPiONwkKd13THYjHH%2BTcQ27zgP2zOnyW%2BsYVAOQvC20WNXnhWg%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