python - 如何从多对多字段中获取所有数据?

标签 python django

我想要在这里做的是显示当前用户“Signels”来自他关注的所有用户。但无法获取超过一个用户的数据。

模型.py

class Signels(models.Model):
     author = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
     coin_name = models.CharField(max_length=300 , null=True, blank=True)
     symbol = models.CharField(max_length=10 , null=True, blank=True)
     title = models.CharField(max_length = 100, null=True, blank=True)
     buy =  models.CharField(max_length=200 , null=True, blank=True)
     sell =  models.CharField(max_length=200 , null=True, blank=True)

class Follow(models.Model):
     user = models.ForeignKey(User, related_name='current_user', null=True)
     Following = models.ManyToManyField(User)

View .py

    def post(request):
        user1 = request.user
        user_now = Follow.objects.get(user=user1)
        for followed in user_now.Following.all():
            signel = Signels.objects.filter(author=followed)
            return render(request, 'posts/all.html', { "signels" : signel } ) 

全部.html

        {% for i in signels %}

        <div class="card post_card ">

       <a type="button" href="/profile/{{i.author.username}}" 
     class="username"><b>{{i.author.username}}</b></a>
       <p class="category">{{i.title}} </p>
      <p class="category"><b>{{i.coin_name}}({{i.symbol}})</b></p>
        <b class="buy">buy</b>:{{ i.buy }} ,  &nbsp;
         <b class="sell">sell</b>: {{i.sell}},  &nbsp;


        {% endfor %}

我在“all.html”中得到的结果是,它显示了他在多对多领域中关注的第一个用户的所有信号。但他没有关注其他用户。我希望当前用户看到他关注的所有用户的所有信号。

最佳答案

您可以通过以下方式获取给定用户关注的作者的所有Signels:

Signels.objects.filter(<b>author__follow__user=request.user</b>)

所以在 View 中,我们可以这样写:

def post(request):
    signels = Signels.objects.filter(<b>author__follow__user=request.user</b>)
    return render(request, 'posts/all.html', { "signels" : signels } )

但我不确定您当前的建模是否正确。最好将两个用户链接到一个 Follow 模型,从而让 Follow 充当两个用户之间的“直通”模型。现在,Follow 对象基本上是 User 模型上的额外“代理”,从而创建了一个额外的表。在建模过程中,User 可能会拥有两个(或更多)Follow 对象,每个对象都包含一组 follow。这还将导致额外的JOIN,从而使查询变得有点“难以”评估。

更有效的“跟随”系统可能是:

from django.contrib.auth import get_user_model

class Follow(models.Model):
    follower = models.ForeignKey(
        get_user_model(),
        related_name='current_user',
        on_delete=models.CASCADE,
        related_name='followees'
    )
    followee = models.ForeignKey(
        get_user_model(),
        related_name='current_user',
        on_delete=models.CASCADE,
        related_name='followers'
    )

    class Meta:
        unique_together=('followers', 'followee')

在这种情况下,查询是:

Signels.objects.filter(<b>author__followers__follower=request.user</b>)

在这里,用户不能关注同一用户两次,并且我们防止通过多对多关系创建额外的“隐藏”表。

或者你可以 create a custom user model [Django-doc]然后您可以在其中添加多对多关系以进行后续操作。

关于python - 如何从多对多字段中获取所有数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54060837/

相关文章:

python - 套接字错误 : [Errno 32] Broken pipe

jquery - 如何在动态表单中设置 .submit jquery 函数?

python - 部署 Django 项目时 Vercel CLI Python 版本问题

python - 在 Pycharm 上流浪调试 python/django

python - App Engine 上的 Pyramid 获取 "InvalidResponseError: header values must be str, got ' unicode'

python - 从两个列表创建嵌套列表

Python PIL 从 STDIN 读取 PNG

java - 什么样的事情Java能做而Python不能做?

django - “用户”对象没有属性 'UserProfile'

javascript - Tornado、Django 和推送通知