Z
Z
zelsky2015-07-19 01:41:29
Django
zelsky, 2015-07-19 01:41:29

Unclear response when querying Djnago?

Everything seems ok returns me

[<UserLikes: UserLikes object>, <UserLikes: UserLikes object>]

For one user in the table with id 3 and returns 1 object for the user with id 2.
But when I want to use like {{all_likes.id_post}} I don't get anything in response, what could be the problem?
current_user = int(request.user.id)
   liked_posts = UserLikes.objects.filter(user_id=current_user)

When so objects come.
<center>{{all_likes}} </center>
class UserLikes(models.Model):
    user_id = models.IntegerField(blank=True,default=1)
    id_post= models.IntegerField(blank=True)

+----+---------+---------+
| id | user_id | id_post |
+----+---------+---------+
| 1 | 3 | 24 |
| 2 | 3 | 25 |
| 3 | 2 | 25 |
+----+---------+---------+
I'm going to check.
{% if one_post.pk in all_likes.id_post %}
Into table
{% else %}
Out of table
{% endif %}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
stry3, 2015-07-19
@stry3

1. While reading the question, I broke my tongue five times and my eyes three times.
2. Please try PHP. In Python, programmers who can't write well in their native language don't survive. Or try to formulate your question in any other language (including Korean). I'm sure you'll be more willing to help.
3. This code will probably work like this:

# views.py
liked_post_ids = UserLikes.objects.filter(user=request.user).values_list('id', flat=True)

# template.html
{% if post.id in liked_posts_ids %}
    This post was already liked.
{% else %}
    You can like this post.
{% endif %}

4. Your UserLikes model (and according to the guideline, it’s still more correct to call it UserLike) is nothing more than a ManyToMany between the Post and User model. Therefore, it's better to do this:
# models.py
from django.contrib.auth.models import User
from django.db import models

class Post(models.Model):
    user = models.ManyToManyField(User)
    slug = models.SlugField()

Another option is through two ForeignKeys (the same ManyToMany, only with a "manual" table). Just don't do it by hand using "models.IntegerField(blank=True, default=1)". Let the base think for you.

S
sim3x, 2015-07-19
@sim3x

You got a bunch of misconceptions about the architecture
request.user - indicates the current authorized user,
I logged in, and through request.user I can filter what I did.
The appearance of such fields indicates that something went wrong

user_id = models.IntegerField()
id_post= models.IntegerField()

int is for storing integers
Don't check in the template
When your query doesn't give you what you think it should return, do a mental experiment.
Get user id = 2 from request.user
Make query select * from tablename where user_id = 2
Return | 3 | 2 | 25 |
that's right
Maybe I want something wrong or do something wrong
Put your likes on the shelf and go through the tutorial again https://docs.djangoproject.com/en/1.8/intro/tutorial01/
Learn it to such an extent, to write a copy of it yourself
After try to play with templates and view
After try to add or remove something from the models
Move on to a new one only after you can confidently say what you did and why.
If at some stage you can’t find a solution for too long, write a question

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question