How to make Django queries with associated tables? - python

I'm trying to create a 'saved post' feature on a website. I'm struggling with how to create a query that I can use to populate my HTML template with posts.
Here are my models:
class User(AbstractUser):
pass
class Post(models.Model):
title = models.CharField(max_length=200)
description = models.CharField(max_length=500)
class SavedPost(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE)
user = models.ForeignKey (User, on_delete=models.CASCADE, null=True)
My .views looks like this
def savedpostsview(request):
posts = Post.objects.all
savedposts = posts.savedpost_set(user = request.user)
return render(request, "posts/savedposts.html",{
'savedposts': savedposts
})
Right now I'm getting the error "'function' object has no attribute 'savedpost_set'".
I know I'm getting something wrong syntactically, but I've been reading documentation forever and can't figure out what it is for the life of me. Does anybody have any insight into what I'm doing wrong?

first of all here Post.objects.all all() is a function and thats why error is "'function' object has no attribute 'savedpost_set'"
You should call Post.objects.all() this will return queryset.
Then You are trying to reverse query on queryset which not possible and will throw error.
All you want is this Post.objects.filter(savedpost__user=request.user)

Related

operator does not exist: character varying = integer

I am building a BlogApp and I was working on a feature and I am stuck on a error.
operator does not exist: character varying = integer
LINE 1: ...d" = "taggit_tag"."id") WHERE "taggit_tag"."name" IN (SELECT...
I am trying to retrieve all the comments commented by user from Tags which were used in comment's post.
When I access the comments then it is keep showing that error when i access the variable in template.
models.py
class Post(models.Model):
post_user = models.ForeignKey(User, on_delete=models.CASCADE)
post_title = models.CharField(max_length=30)
tags = models.TaggableManager()
class Comment(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
post_of = models.ForeignKey(Post, on_delete=models.CASCADE)
views.py
class page(request):
tagQuery = Tag.objects.filter(post__comment__user=request.user)
#this is showing error
subquery = Comment.objects.filter(post_of__tags__name__in=tagQuery)
context = {'subquery':subquery}
return render(request, 'page.html', context)
It was showing
The QuerySet value for an exact lookup must be limited to one result using slicing.
So i used __in but then it keep showing that error.
Any help would be much Appreciated. Thank You
Rather than filtering according to queryset itself, you need to filter according to values of certain field:
class page(request):
tagQuery = Tag.objects.filter(post__comment__user=request.user)
subquery = Comment.objects.filter(post_of__tags__name__in=tagQuery.values_list('name'))
context = {'subquery':subquery}
return render(request, 'page.html', context)

'QuerySet' object has no attribute 'likes'

I am implementing a likes system for my post. I have used a ManyToManyField in my model.
models.py
class MarketingMaestro (models.Model):
product_title = models.CharField(max_length=250)
total_value = models.IntegerField()
branding_video = models.CharField(max_length=300)
likes = models.ManyToManyField(User, related_name='likes', blank=True)
In this I get all the list of the users, and if someone likes the post it is updated in the field. But when I am trying to implement the logic where if the user has already liked the post he/she will be able to see the dislike button only.
While doing so I am fetching all the values from the db and checking particular likes field but it is showing error 'QuerySet' object has no attribute 'likes'
It is giving me error on the line
if marking_maestro.likes.filter(id=request.user.id).exists():
I tried printing the value but is it returning just the queryset <QuerySet [<MarketingMaestro: MarketingMaestro object (1)>]> and if i print the product_title using (marking_maestro.product_title) but then I get the error saying 'QuerySet' object has no attribute 'product_title'
-views.py
def brand(request):
# fetching all the projects
marking_maestro = MarketingMaestro.objects.all()
is_liked = False
print(marking_maestro)
if marking_maestro.likes.filter(id=request.user.id).exists():
is_liked = True
# fetching the votes
emailID = request.user.email
context = {
'is_liked' : is_liked
}
return render(request, 'brand.html', {'marking_maestro' : marking_maestro}, context)
for performing queries like marking_maestro.likes.filter(id=request.user.id).exists(): or marking_maestro.product_title you need to first get an object of MarketingMaestro.
Here, only mistake you are doing is that, you are performing queries on QuerySet rather than MarketingMaestro object.
So what you need to do is
marking_maestro = MarketingMaestro.objects.first()
(here I've taken first to just illustrate the concept, you can take whatevet the object you want)
and then perform whatever the queries you want to.

Error when adding many-to-many-field on Post

I have an app within my project called posts, where inside their in the models.py, I have two models: Post and Like.
I want to add a many-to-many-field on the post that references the Like model.
I have executed makemigrations and migrate, however I am getting this error:
NameError: name 'Like' is not defined
models.py:
class Post(models.Model):
file = models.ImageField(upload_to='images/')
summary = models.TextField(max_length=600)
pub_date = models.DateTimeField(auto_now=True)
user = models.ForeignKey(User, on_delete=models.CASCADE)
likes = models.ManyToManyField(Like)
def __str__(self):
return self.user.username
def summary_pretty(self):
return self.summary[:50]
def pub_date_pretty(self):
return self.pub_date.strftime('%b %e %Y')
class Like(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
status = models.BooleanField(default=False)
it says
NameError: name 'Like' is not defined
because you must move Like model above Post model it hasn't been defined yet in your code in that line (python code is interpreted)
and it's not a good thing to design database models like that based on:
Why should I avoid loops when designing relationships for a database?
so delete the Post foreign key in Like model and you can retrieve a like's post with reverse lookup
you can find more about it in django official docs:
Lookups that span relationships
and also
Many-to-many relationships
You're referencing the class Like before initiating it in your Python file. Hence "Like" is not defined.
likes = models.ManyToManyField(Like)
You need to delete "likes" from your Post class. That will fix the error.
As for your code, I think you're misunderstanding why we use Intermediary Tables. You don't need to reference Like in your Post class. You've already established that relationship in the Like Class:
post = models.ForeignKey(Post, on_delete=models.CASCADE)
Retrieving the users who have liked a post is as simple as writing a filter on the Likes table.
#views.py
from models import Post, Like
post_id = 1
likes = Like.objects.filter(post=post_id)

Like functionality in Django

I'm developing a social platform and currently coding the like functionality for user posts. However, I can't seem to make it work. These are my Models.py:
class Post(models.Model):
user = models.ForeignKey(User)
posted = models.DateTimeField(auto_now_add=True)
content = models.CharField(max_length=150)
picturefile = models.ImageField(upload_to="post_content", blank=True)
class Like(models.Model):
user = models.ForeignKey(User, null=True)
post = models.ForeignKey(Post, null=True)
I pass the post ID through my url as 'post_id', and then in my views:
def liking(request, post_id):
newlike = Like.objects.create()
newlike.post = post_id
newlike.user = request.user
newlike.save()
return redirect(reverse('dashboard'))
However, it returns the following error:
Cannot assign "'47'": "Like.post" must be a "Post" instance.
Does anyone knows what I'm missing or doing wrong?
You are passing newlike.post a number (integer field) while it is expecting a Post instance.
This sould work:
from django.http.shortcuts import get_object_or_404
def liking(request, post_id):
post = get_object_or_404(Post, id=post_id)
newlike = Like.objects.create(user=request.user, post=post)
return redirect(reverse('dashboard'))
Note 1: Better use the handy shortcut get_object_or_404 in order to raise a 404 error when the specific Post does not exist.
Note 2: By calling objects.create will automatically save into the db and return an instance!
newlike.post should be a Post object, not an int.
You need to find post by id first:
post = Post.objects.get(pk=post_id)
newlike.post = post
or, if you don't want to do this lookup:
newlike.post_id = post_id

Django View/Model Attribute Error

I'm new to Django and I'm in the process of converting a PHP project into Python etc.
I'm trying to do something super simple, but I keep getting the following error:
AttributeError at /news/1/
'QuerySet' object has no attribute 'slug'
Here is my most of my Model to help explain:
class Article(models.Model):
title = models.CharField(max_length=200)
STATUS_CHOICES = ((1,'Published'), (2,'Hidden'), (3,'Draft'))
status = models.IntegerField(choices=STATUS_CHOICES,default=3)
pub_date = models.DateField('date published')
tags = TaggableManager()
header_width = models.IntegerField(default=1,blank=True,null=True)
header_height = models.IntegerField(default=1,blank=True,null=True)
header = models.ImageField(upload_to='news/',width_field='header_width',height_field='header_height',blank=True,null=True)
header_filter = models.BooleanField('Enable filter',default=1)
excerpt = HTMLField(blank=True)
body = HTMLField(blank=True)
custom_link_text = models.CharField(max_length=20,default="Read More")
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
slug = AutoSlugField(unique=True,max_length=200,populate_from='db_slug',default="",slugify=return_value)
def __str__(self):
return self.title
Im currently just testing to pull through the slug, so my view method currently looks like this:
def detail_redirect(request, pk):
a = Article.objects.all().filter(pk=pk)
return HttpResponse(a.slug)
# return redirect('news:detail',args=(a.slug,pk))
The plan is for this method to redirect to another URL in my application. It queries the DB via the primary key and fetches the Slug, which I then pass on to the redirect shortcut.
It seems to be something that should just work, but it's not. It's really frustrating. The object i query appears to return ok. Because of my __str__ method it returns the title. But any other attributes throw and error. Could it be todo with visibility such as being private or protected?
I'm hoping it's something simple I'm missing. Let me know if you require more code/detail to help explain.
Thanks for taking the time to look at my question.
filter always returns a queryset, which is a list-like object potentially consisting of many items. Querysets do not have model attributes, only their members do. You should use get instead:
a = Article.objects.get(pk=pk)
(Note you don't need all(), in either version of the code.)

Categories