在Django中,多对多关系为何在管理面板中显示错误数据

huangapple go评论72阅读模式
英文:

Why in Django many-to-many relation shows wrong data in the admin panel

问题

I'm trying to implement the many-to-many relation to support in the user model these features:

  1. user followers (persons who follows the user)
  2. user following (persons the user follows)

To achieve that I'm using double relation on the same model because followers/following are the same User models.
Basically it works, however, I'm facing a weird behavior in the Django admin panel. When I add some user to another user's following list (in the shell, everything is okay), the Django admin panel shows that user in the followers list, but they should be in the following list. I can't understand why it happens.

Many thanks for any help!

My models.py

from django.contrib.auth.models import AbstractUser
from django.db import models

class User(AbstractUser):
    followers = models.ManyToManyField('self', blank=True, related_name='user_followers', symmetrical=False)
    following = models.ManyToManyField('self', blank=True, related_name='user_following', symmetrical=False)
    pass

Suppose Admin wants to follow Neo. Neo should have a follower - Admin. Admin should have Neo in his following list.

Shell commands and results:

admin = User.objects.filter(username='admin')[0]
neo = User.objects.filter(username='neo_anderson')[0]

# Check that everything is empty
neo.user_following.all()  # []
neo.user_followers.all()  # []

# --- Admin wants to follow Neo ---

# Add Admin to Neo's followers
neo.user_followers.add(admin)
neo.save()

# Add Neo to Admin's following
admin.user_following.add(neo)
admin.save()

# Check if Admin follows Neo
admin.user_following.contains(neo)  # True

# Check if Neo has the follower Admin
neo.user_followers.contains(admin)  # True

But in the admin panel, I see that for the user Admin, the user Neo is in the followers section, but it should be in the following section because Admin wants to follow Neo, not vice versa.

英文:

I'm trying to implement the many-to-many relation to support in the user model these features:

  1. user followers (persons who follows the user)
  2. user following (persons the user follows)

To achieve that I'm using double relation on the same model because followers/following are the same User models.
Basically it works hoverwer I'm facing a weird behavior in the Django admin panel. When I add some user to other user to the following list (in the shell everything ok), the Django admin panel shows that user in the followers list but he should be in the following list. I can't understand why it happens.
<br><br>Many thanks for any help!
<br /><br>
My models.py

from django.contrib.auth.models import AbstractUser
from django.db import models


class User(AbstractUser):
    followers = models.ManyToManyField(&#39;self&#39;,blank=True,related_name=&#39;user_followers&#39;,symmetrical=False)
    following = models.ManyToManyField(&#39;self&#39;,blank=True,related_name=&#39;user_following&#39;,symmetrical=False)
    pass

Supppose Admin wants to follow Neo. Neo should have follower - Admin. Admin - should have Neo in his following list.
Shell commands and result:

admin = User.objects.filter(username=&#39;admin&#39;)[0]
neo = User.objects.filter(username=&#39;neo_anderson&#39;)[0]


// check that everything is empty
neo.user_following.all()
&lt;QuerySet []&gt;
neo.user_followers.all()
&lt;QuerySet []&gt;


// --- admin wants to follow neo ---

// add the admin to the neo followers
 neo.user_followers.add(admin)
 neo.save()

// add the neo to the admin following 
admin.user_following.add(neo)
admin.save()

// check if admin follows Neo
admin.user_following.contains(neo)  
TRUE

// check if neo has the follower admin
neo.user_followers.contains(admin)      
TRUE

But in the admin panel I see that for the user Admin the user Neo is in the followers section but it should be following because Admin wants to follow Neo. Not vice versa

在Django中,多对多关系为何在管理面板中显示错误数据

答案1

得分: 1

首先使用:
admin = User.objects.get(username='admin')以避免生成一个需要索引的列表...(更干净)

其次:
我不确定"related_name"属性的确切工作方式以及它的行为如何,但你尝试过切换/使用相反的吗?我会假设"followers"应该关注"我",意味着它们具有"following"属性,而不是"followers"属性!?

也许我错了,但尝试:

followers = models.ManyToManyField('self', blank=True, related_name='user_following', symmetrical=False)
following = models.ManyToManyField('self', blank=True, related_name='user_followers', symmetrical=False)

(记得将这些更改迁移到数据库)

我很好奇那时的行为如何,请告诉我。

英文:

First of all use:
admin = User.objects.get(username='admin') to avoid having a list, that you need to index... (cleaner)

Second:
I am not sure how the "related_name" property is working exactly and how it would behave, but have you tried switching / using the opposite? I would assume followers would follow "me", meaning they have the following attribute, instead of the followers attribute!?

Maybe I am wrong, but try:

followers = models.ManyToManyField(&#39;self&#39;,blank=True,related_name=&#39;user_following&#39;,symmetrical=False)
following = models.ManyToManyField(&#39;self&#39;,blank=True,related_name=&#39;user_followers&#39;,symmetrical=False)

(remember to migrate those changes to the database)

I would be curious what the behavior is then, please let me know

huangapple
  • 本文由 发表于 2023年8月9日 15:37:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76865563-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定