Django Queryset for Getting Feeds from Following Users - python

I am trying to get feeds from following users such as Facebook, Instagram, and Twitter. I wrote a view to show feeds from following people.
class Following(View):
def get(self, request):
user = request.user
posts = models.Post.objects.filter(creator__in=user.following.all()).order_by('created_date')[:5]
I was worrying if it might cause a performance issue due to user.following.all() part. I do not know why
filter(creator=user.following)
does not work.
following is a self-referencing ManyToManyField.
followers = models.ManyToManyField("self", blank=True)
following = models.ManyToManyField("self", blank=True)
Is there any better solution?
I have another issues with counting a number of ManyToManyFields.
class Popular(View):
def get(self, request):
user = request.user
posts = models.Post.objects.annotate(number_of_likes=Count('likes'), number_of_comments=Count('comments')).filter(Q(channels__users=user) & (Q(number_of_likes__gte=5) | Q(number_of_comments__gte=5))).order_by('created_date')[:5]
It is so long because likes__count and comments__count did not work. The only case I found on the internet that works was this, using annotate(). Is there a better solution for this? I want to filter out posts that have channels a user is subscribing and among them, I want to only show number of likes or number of comments are above certain range. Why does this not work?
models.Post.objects.filter(Q(channels__users=user) & (Q(likes__count__gte=5) | Q(comments__count__gte=5))).order_by('created_date')[:5]
class Comment(news_models.TimeStampedModel):
post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='comments')
class Like(news_models.TimeStampedModel):
comment = models.ForeignKey(Comment, null=True, on_delete=models.CASCADE, related_name='likes')
class Channel(models.Model):
name = models.CharField(max_length=100, blank=False, unique=True)
users = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name='channels')
posts = models.ManyToManyField(Post, related_name='channels')

Related

How to filter by BooleanField in Django?

I have been having a hard time accomplishing this task and it seems that I cannot get help anywhere I turn. I am trying to send Memos to specific user groups in Django. Each user in each group should receive the Memo and be able to change the BooleanField to True to signify that they have read it.
I then need to be able to access the amount of users which received and have marked the BoolenField as True so I can create a table in a template which says [3/31 Read] and such.
I would like to not use GenericForeignkeys if possible and I would like to keep it as one Model if possible but I know that may not work. Currently I was trying this:
class Memo(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
date_time = models.DateTimeField(default=timezone.now)
sender = models.ForeignKey(User, on_delete=models.CASCADE)
receiver = models.ManyToManyField(Group)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('memos-detail', kwargs={'pk': self.pk})
I was going to access each user (receiver) within the group which is selected on the MemoCreateForm then apply that user to this model:
class ReceivedMemo(models.Model):
memo = models.ForeignKey(
Memo, related_name='user_memos', on_delete=models.CASCADE)
receiver = models.ForeignKey(
User, related_name='memos', on_delete=models.CASCADE)
read = models.BooleanField(default=False)
Then I was going to try to filter the ReceivedMemos by memo to see if each receiver has read the memo or not. But this is starting to get complicated and I am not sure if it will work. Am I going about this the right way? Or should I be able to have one Model such as:
class Memo(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
date_time = models.DateTimeField(default=timezone.now)
sender = models.ForeignKey(User, on_delete=models.CASCADE)
receiver = models.ForeignKey(User, on_delete=models.CASCADE)
read = models.BooleanField(default=True)
Seems that each object would have the BooleanField applied to the object and not the user though.
The ReceivedMemo model seems more appropriate rather than the read bit, but the issue with this approach is that whenever you create a new Memo you need to also create lots of (for every User in the Group) ReceivedMemo objects with read=False? This seems pointless. Maybe you can just store the Users which actually read this thing, and for everyone that's left, consider he has not read it. I.e.
class Memo(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
date_time = models.DateTimeField(default=timezone.now)
sender = models.ForeignKey(User, on_delete=models.CASCADE)
receiver = models.ManyToManyField(Group)
read_by = models.ManyToManyField(User)

Django follow feature with m2m

I tried to solve the problem and got stuck. The problem is that I have a post that I can follow. My problem is that I don't know how to add a tracking button. Should this be done by url, with a view? Or should it be rather as a method in the model?
My problem is also whether it is properly written in terms of models - using the intermediate model Follower?
Here is Post model and I would like to add followers here. I mean, everybody who is interested, can follow this post.
class Post(models.Model):
owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='posts')
title = models.CharField(max_length=255, unique=True)
description = models.TextField(max_length=1024)
followers = models.ManyToManyField(settings.AUTH_USER_MODEL, through='Follower', blank=True)
is_visible = models.BooleanField(default=True)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('posts:post_detail', kwargs={'pk': self.pk})
def number_of_followers(self):
return self.followers.count()
Here is my manager for follower model:
class FollowerManager(models.Manager):
use_for_related_fields = True
def follow(self, user, pk):
post_object = get_object_or_404(Post, pk=pk)
if user.is_authenticated():
if user in post_object.followers.all():
Follower.objects.filter(post=post_object, user=user).delete()
else:
Follower.objects.create(post=post_object, user=user)
Here is Follower model:
class Follower(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
post = models.ForeignKey(Post, on_delete=models.CASCADE)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
objects = FollowerManager()
Interactions between a user's browser and the database can only be done via a URL and a view. That view might call a model method, but there is no possible way for the browser to call that method directly.
(Also I don't understand what you're doing in the manager. Why are you deleting followers if the user is authenticated? Note that will always be true, so the followers will always be deleted.)

Get site-specific user profile fields from user-created object

I am using Django sites framework (Django 2.1) to split an app into multiple sites. All of my models except the User model are site-specific. Here is my Post model:
post.py
class Post(models.Model):
parent = models.ForeignKey(
'self',
on_delete=models.CASCADE,
related_name='children',
related_query_name='child',
blank=True,
null=True,
)
title = models.CharField(
max_length=255,
blank=True,
)
body_raw = models.TextField()
body_html = models.TextField(blank=True)
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
)
site = models.ForeignKey(Site, on_delete=models.CASCADE)
on_site = CurrentSiteManager()
I have no problem separating posts out by site. When I want to get the posts, I call:
posts = Post.on_site.filter(...)
I have a separate model called UserProfile. It is a many-to-one profile where there is a unique profile created for each user-site combination (similar to profile implementation at SE). The profile has a reputation attribute that I want to access when I get any post. This reputation attribute should be different for each site (like how on SE you have different rep on each site you are a member of).
user_profile.py
class UserProfile(models.Model):
user = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
reputation = models.PositiveIntegerField(default=1)
site = models.ForeignKey(Site, on_delete=models.CASCADE)
on_site = CurrentSiteManager()
How do I access the user's username (on the User model) as well as the user's reputation (on the UserProfile model) when I get Posts from a query?
I'd like to do something like:
Post.on_site.filter(...)
.select_related('user__userprofile')
.filter_related(user__userprofile.site == get_current_site())
How do I filter a Many-To-One related model?
Better to make UserProfile -> User relationship to be OnetoOne,
because Django doesn't know which of many profiles to show
(but you also need to define related_name)
models.OneToOneField(get_user_model(), related_name='userprofile_rev')
Then you will be able to do this
qs = Post.on_site.filer().select_related('user', 'user__userprofile_rev')
for post in qs:
print(post.user.username, post.user.userprofile_rev.reputation)
If you don't want to change your DB structure you can do like this
(but you need to specify which profile to return)
qs = Post.on_site.filer().select_related('user').prefetch_related('user__userprofile_set')
for post in qs:
print(post.user.username, post.user.userprofile_set[0].reputation)

How to setup many to many one to many and many to one relationship in django

From the last seven days I am trying to build a blog using django and I figured out the model for my blog as follows
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=200)
body = models.TextField()
created_at = models.DateTimeField('created date', auto_now_add=True, auto_now=False)
updated_at = models.DateTimeField('updated date', auto_now_add=False, auto_now=True)
def __str__(self):
return self.title
class Author(models.Model):
name = models.CharField(max_length=150)
email = models.EmailField(blank=True)
bio = models.TextField()
#This line define many author can have a post
#but I want manypost can only have single author.
#in my view to accomplish this i need to define author in Post class
#on defining author=ForeignKey(Author) in post, It throws an error Author is not defined
post = models.ForeignKey(Post)
def __str__(self):
return self.author
class Category(models.Model):
cat_name = models.CharField(max_length=200)
post = models.ManyToManyField(Post)
def __str__(self):
return self.cat_name
The thing that I am so much confused is the relation between the Post, Categories, Author and Tag.
Relation What I wanted to set is
author can have many posts, A post can have only single author
categories can have many posts, A post can have many categories
a tag can have many posts a post can have many tags
But the models that I am created above is not working as I expected(I am so confused).
when I put the author field in Post Class, so that i would be a relation like, an author can have many posts, but I got the error Author is not defined.(as I know the interpreter run code from top to bottom). How do I accomplish
You have two options:
Change Author and Post position, thus Page can see Post definition.
Use Lazy call as: author= models.ForeignKey("Author")
In this way, Django will wait until all models load then resolve the dependencies.
Try model structure syntax:
class Author(models.Model):
name = models.CharField(max_length=150)
email = models.EmailField(blank=True)
bio = models.TextField()
def __str__(self):
return self.author
class Post(models.Model):
author = models.ForeignKey(Author)
title = models.CharField(max_length=200)
body = models.TextField()
created_at = models.DateTimeField('created date', auto_now_add=True, auto_now=False)
updated_at = models.DateTimeField('updated date', auto_now_add=False, auto_now=True)
def __str__(self):
return self.title
class Category(models.Model):
cat_name = models.CharField(max_length=200)
post = models.ManyToManyField(Post)
def __str__(self):
return self.cat_name
class Tag(models.Model):
tag_name = models.CharField(max_length=200)
post = models.ManyToManyField(Post)
def __str__(self):
return self.tag_name
This will solve your three requirements:
1.) Author can have many posts because I set the Post Model into one-to-many field
2.) Categories can have many posts vice versa (This is already set according to your code)
3.) Tag can have many posts vice versa (Very similar to your already made Category Model where I made the field many-to-many between the two)
The reason why you got an error is because you made your author to post relation to many-to-one (many authors in one post) according to your model structure
Now to test it simply migrate all these changes and quickly test it in your admin page
author can have many posts, A post can have only single author
Post model should have a ForeignKey pointing to Author.
class Post(...):
author = models.ForeignKey('Author')
categories can have many posts, A post can have many categories
Post model should have a ManyToManyField pointing to Category.
class Post(...):
category = models.ManyToManyField('Category')
a tag can have many posts a post can have many tags
Same as number 2.

Django getting especific object using QuerySet in reverse relationship many-to-one

Lets say I have the following models:
Chat(models.Model):
community = models.ForeignKey(Community, related_name="chats", null=True, blank=True)
....
Community(models.Model):
slug = models.CharField(max_length=250, unique=True, default='')
....
Profile(models.Model):
public_name = models.CharField(max_length=20, unique=True)
community_subscriptions = models.ManyToManyField('Community', through='CommunitySubscription')
chat_subscriptions = models.ManyToManyField('Chat', through='ChatSubscription')
....
ChatSubscription(models.Model):
profile = models.ForeignKey(Profile, unique=False, related_name='chat_subscription')
chat = models.ForeignKey(Chat, null=True, blank=True, related_name='chat_subscribers')
....
CommunitySubscription(models.Model):
....
In a view I want to do the following:
profileA = Profile.objects.get(public_name=public_nameA)
profileB = Profile.objects.get(public_name=public_nameB)
community = Community.objects.get(slug=community_slug)
try:
# I want to get the unique chat that involves both profiles for a specific community or get DoesNotExist (or None) if there is not such chat.
chat = Chat.objects.????????????
except Chat.DoesNotExist:
....
If the chat exist then both profiles will have a ChatSubscription object relating the profile with the chat.
Is it possible to do this kind of filtering using Django QuerySets in one single line? If not what would be the most efficient way to do it in more than one line?
Thanks a lot in advance.
Pretty complex, so try using the Q object to do a 'AND' query (assuming I got what you're trying to do):
from django.db.models import Q
Chat.objects.filter(Q(chatsubscription__profile__in=[profileA,profileB]) & Q(community=communty))

Categories

Resources