如果我们正在访问多个 类 来实现这一点,如何加快 Django 中大记录的查询事务?
How to speed up the query transactions for large records in Django, if we are visiting multiple classes to achieve this?
我正在尝试根据标签 he/she 显示已登录用户的用户配置文件建议,在我的情况下,我必须前往 User_Interests_Tag class 才能获得所有标签,接下来访问 Post_Tag class 以获取每个标签(循环)的所有帖子,一旦我拥有所有 postId,请访问 Post class 以获取每个唯一用户并将其附加到配置文件(列表)并使用分页器来限制发送给客户端的配置文件。
我如何在不使用 for 循环来提高服务器的整体效率和响应时间的情况下实现这一点?
型号:
class User_Interests_Tag(models.Model): # User Suggestions Tag to User link
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, unique=True)
user = models.ForeignKey(User, null=False, on_delete=models.CASCADE)
tag = models.ForeignKey(Tag, null=False, on_delete=models.CASCADE)
priority = models.FloatField(default=0.0)
class Post_Tag(models.Model): # Post Media Link
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, unique=True)
post = models.ForeignKey(Post, on_delete=models.CASCADE)
tag = models.ForeignKey(Tag, on_delete=models.CASCADE)
class Post(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, unique=True)
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
title= models.CharField(max_length=500, default="unknown", null=True)
description = models.CharField(max_length=512 , null = True)
date_posted = models.DateTimeField(auto_now_add=True)
这是我想要实现的代码片段:
#looping through each tag
for user_interests_tag in user_interests_tags:
#user_post_tags = Post_Tag.objects.in_bulk(user_interests_tags) (used bulk but no luck)
user_post_tags.extend(list(Post_Tag.objects.filter(tag_id = user_interests_tag).values_list('post_id', flat=True)))
profiles = []
#looping through each tag id
for user_post_tag in user_post_tags:
#getting the user from post class
user = Post.objects.get(id = user_post_tag)
#checking if already follows and if already their in user profiles array
if user not in profiles and not User_Follow.objects.filter( owner_id = user_id.id, user_id = user.user_id):
profiles.append(user)
#paginating 6 per request
paginator = Paginator(profiles,6)
limited_profiles = paginator.page(page)
return limited_profiles
非常感谢任何线索,提前致谢:)
你可以这样做:
posts = Post.objects.filter(
Q(post_tag_set__tag__in=user_interests_tags)
& ~Q(user__in=User_Follow.objects.filter(owner_id=user_id.id).values('user'))
).distinct()
paginator = Paginator(posts, 6)
limited_profiles = paginator.page(page)
return limited_profiles
对此进行扩展:
Q(post_tag_set__tag__in=user_interests_tags)
以上将过滤所有具有 Post_Tag
且 Tag
匹配 user_interests_tags
.
的所有 Post
~Q(user__in=User_Follow.objects.filter(owner_id=user_id.id).values('user'))
以上内容还将根据您当前拥有的所有者 ID 筛选器,用 user
筛选不匹配任何 User_Follow
对象的 Post
。
我正在尝试根据标签 he/she 显示已登录用户的用户配置文件建议,在我的情况下,我必须前往 User_Interests_Tag class 才能获得所有标签,接下来访问 Post_Tag class 以获取每个标签(循环)的所有帖子,一旦我拥有所有 postId,请访问 Post class 以获取每个唯一用户并将其附加到配置文件(列表)并使用分页器来限制发送给客户端的配置文件。 我如何在不使用 for 循环来提高服务器的整体效率和响应时间的情况下实现这一点?
型号:
class User_Interests_Tag(models.Model): # User Suggestions Tag to User link
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, unique=True)
user = models.ForeignKey(User, null=False, on_delete=models.CASCADE)
tag = models.ForeignKey(Tag, null=False, on_delete=models.CASCADE)
priority = models.FloatField(default=0.0)
class Post_Tag(models.Model): # Post Media Link
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, unique=True)
post = models.ForeignKey(Post, on_delete=models.CASCADE)
tag = models.ForeignKey(Tag, on_delete=models.CASCADE)
class Post(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, unique=True)
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
title= models.CharField(max_length=500, default="unknown", null=True)
description = models.CharField(max_length=512 , null = True)
date_posted = models.DateTimeField(auto_now_add=True)
这是我想要实现的代码片段:
#looping through each tag
for user_interests_tag in user_interests_tags:
#user_post_tags = Post_Tag.objects.in_bulk(user_interests_tags) (used bulk but no luck)
user_post_tags.extend(list(Post_Tag.objects.filter(tag_id = user_interests_tag).values_list('post_id', flat=True)))
profiles = []
#looping through each tag id
for user_post_tag in user_post_tags:
#getting the user from post class
user = Post.objects.get(id = user_post_tag)
#checking if already follows and if already their in user profiles array
if user not in profiles and not User_Follow.objects.filter( owner_id = user_id.id, user_id = user.user_id):
profiles.append(user)
#paginating 6 per request
paginator = Paginator(profiles,6)
limited_profiles = paginator.page(page)
return limited_profiles
非常感谢任何线索,提前致谢:)
你可以这样做:
posts = Post.objects.filter(
Q(post_tag_set__tag__in=user_interests_tags)
& ~Q(user__in=User_Follow.objects.filter(owner_id=user_id.id).values('user'))
).distinct()
paginator = Paginator(posts, 6)
limited_profiles = paginator.page(page)
return limited_profiles
对此进行扩展:
Q(post_tag_set__tag__in=user_interests_tags)
以上将过滤所有具有 Post_Tag
且 Tag
匹配 user_interests_tags
.
Post
~Q(user__in=User_Follow.objects.filter(owner_id=user_id.id).values('user'))
以上内容还将根据您当前拥有的所有者 ID 筛选器,用 user
筛选不匹配任何 User_Follow
对象的 Post
。