How to get object and many to many object information
up vote
1
down vote
favorite
I want to get information: if request user vote for this project.
My Project class:
class Project(TimestampedModel):
title = models.CharField(max_length=120)
description = models.TextField()
company = models.ForeignKey(
Company, on_delete=models.CASCADE
)
tags = models.ManyToManyField(Tag)
votes = models.ManyToManyField(User)
My query, I tried get it by annotate and filter inside, but not work
queryset = Project.objects.all()
if 'title' in filters.keys() and filters['title'] is not None:
queryset = queryset.filter(title__icontains=filters['title'])
queryset = queryset.annotate(num_votes=Count("votes"))
queryset = queryset.annotate(user_vote=Count("votes__user_id", filter=Q(votes__user_id=filters['user_id'])))
queryset = queryset.order_by('-created_at')
python django
add a comment |
up vote
1
down vote
favorite
I want to get information: if request user vote for this project.
My Project class:
class Project(TimestampedModel):
title = models.CharField(max_length=120)
description = models.TextField()
company = models.ForeignKey(
Company, on_delete=models.CASCADE
)
tags = models.ManyToManyField(Tag)
votes = models.ManyToManyField(User)
My query, I tried get it by annotate and filter inside, but not work
queryset = Project.objects.all()
if 'title' in filters.keys() and filters['title'] is not None:
queryset = queryset.filter(title__icontains=filters['title'])
queryset = queryset.annotate(num_votes=Count("votes"))
queryset = queryset.annotate(user_vote=Count("votes__user_id", filter=Q(votes__user_id=filters['user_id'])))
queryset = queryset.order_by('-created_at')
python django
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I want to get information: if request user vote for this project.
My Project class:
class Project(TimestampedModel):
title = models.CharField(max_length=120)
description = models.TextField()
company = models.ForeignKey(
Company, on_delete=models.CASCADE
)
tags = models.ManyToManyField(Tag)
votes = models.ManyToManyField(User)
My query, I tried get it by annotate and filter inside, but not work
queryset = Project.objects.all()
if 'title' in filters.keys() and filters['title'] is not None:
queryset = queryset.filter(title__icontains=filters['title'])
queryset = queryset.annotate(num_votes=Count("votes"))
queryset = queryset.annotate(user_vote=Count("votes__user_id", filter=Q(votes__user_id=filters['user_id'])))
queryset = queryset.order_by('-created_at')
python django
I want to get information: if request user vote for this project.
My Project class:
class Project(TimestampedModel):
title = models.CharField(max_length=120)
description = models.TextField()
company = models.ForeignKey(
Company, on_delete=models.CASCADE
)
tags = models.ManyToManyField(Tag)
votes = models.ManyToManyField(User)
My query, I tried get it by annotate and filter inside, but not work
queryset = Project.objects.all()
if 'title' in filters.keys() and filters['title'] is not None:
queryset = queryset.filter(title__icontains=filters['title'])
queryset = queryset.annotate(num_votes=Count("votes"))
queryset = queryset.annotate(user_vote=Count("votes__user_id", filter=Q(votes__user_id=filters['user_id'])))
queryset = queryset.order_by('-created_at')
python django
python django
asked Nov 10 at 19:17
jgaw
62
62
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
I resolved this.
queryset = queryset.annotate(user_vote=Count("votes__user_id", filter=Q(votes__id=filters['user_id'])))
I didn't know that votes__id is equivalent to user id in this relation
add a comment |
up vote
0
down vote
Try to use related name on a votes field:
votes = models.ManyToManyField(User, related_name='projects')
That way you can access all projects that user voted for by this related_name
user = User.objects.filter(username='john')
user.projects.all() # > all projects that user voted for
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I resolved this.
queryset = queryset.annotate(user_vote=Count("votes__user_id", filter=Q(votes__id=filters['user_id'])))
I didn't know that votes__id is equivalent to user id in this relation
add a comment |
up vote
0
down vote
I resolved this.
queryset = queryset.annotate(user_vote=Count("votes__user_id", filter=Q(votes__id=filters['user_id'])))
I didn't know that votes__id is equivalent to user id in this relation
add a comment |
up vote
0
down vote
up vote
0
down vote
I resolved this.
queryset = queryset.annotate(user_vote=Count("votes__user_id", filter=Q(votes__id=filters['user_id'])))
I didn't know that votes__id is equivalent to user id in this relation
I resolved this.
queryset = queryset.annotate(user_vote=Count("votes__user_id", filter=Q(votes__id=filters['user_id'])))
I didn't know that votes__id is equivalent to user id in this relation
answered Nov 10 at 20:10
jgaw
62
62
add a comment |
add a comment |
up vote
0
down vote
Try to use related name on a votes field:
votes = models.ManyToManyField(User, related_name='projects')
That way you can access all projects that user voted for by this related_name
user = User.objects.filter(username='john')
user.projects.all() # > all projects that user voted for
add a comment |
up vote
0
down vote
Try to use related name on a votes field:
votes = models.ManyToManyField(User, related_name='projects')
That way you can access all projects that user voted for by this related_name
user = User.objects.filter(username='john')
user.projects.all() # > all projects that user voted for
add a comment |
up vote
0
down vote
up vote
0
down vote
Try to use related name on a votes field:
votes = models.ManyToManyField(User, related_name='projects')
That way you can access all projects that user voted for by this related_name
user = User.objects.filter(username='john')
user.projects.all() # > all projects that user voted for
Try to use related name on a votes field:
votes = models.ManyToManyField(User, related_name='projects')
That way you can access all projects that user voted for by this related_name
user = User.objects.filter(username='john')
user.projects.all() # > all projects that user voted for
answered Nov 10 at 22:25
bloodwithmilk
128111
128111
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242543%2fhow-to-get-object-and-many-to-many-object-information%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown