Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 实现新闻提要的有效方法_Python_Django - Fatal编程技术网

Python 实现新闻提要的有效方法

Python 实现新闻提要的有效方法,python,django,Python,Django,我正在尝试建立一个简单的社交网络 假设我有两个模型:Status和我的自定义UserProfile模型来实现追随者/以下功能: from django.contrib.auth.models import User class Status(models.Model): user = models.ForeignKey(User, related_name='statuses') title = models.CharField(max_length=50) statu

我正在尝试建立一个简单的社交网络

假设我有两个模型:
Status
和我的自定义
UserProfile
模型来实现追随者/以下功能:

from django.contrib.auth.models import User

class Status(models.Model):
    user = models.ForeignKey(User, related_name='statuses')
    title = models.CharField(max_length=50)
    status = models.TextField()
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)

    class Meta:
        verbose_name = "Status"
        verbose_name_plural = "Statuses"
        ordering = ('-created',)
        get_latest_by = 'created'


class UserProfile(models.Model):
    user = models.OneToOneField(User, related_name='profile')
    followings = models.ManyToManyField(
        'self', related_name='followers', symmetrical=False, blank=True)
访问新闻提要的最有效方式是什么,即我所有(
用户
以下内容的最近
状态


我是Django的新手。任何帮助都将不胜感激。

只需筛选您正在跟踪的用户设置的
状态。上面的查询将显示所有用户的所有状态,后跟
user

Status.objects.filter(user__followers=user)

谢谢但是你的答案不是100%正确<代码>状态.objects.filter(user\u profile\u followers=user.profile)
执行此任务。这是最有效的方法吗?您还可以像这样创建queryset:
Status.objects.filter(user\uu in=user.objects.filter(profile\uu followers=user.profile))
。但哪一个更好将取决于每个表中的数据量和使用的数据库引擎。