英文:
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:
- user followers (persons who follows the user)
- 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:
- user followers (persons who follows the user)
- 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('self',blank=True,related_name='user_followers',symmetrical=False)
following = models.ManyToManyField('self',blank=True,related_name='user_following',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='admin')[0]
neo = User.objects.filter(username='neo_anderson')[0]
// check that everything is empty
neo.user_following.all()
<QuerySet []>
neo.user_followers.all()
<QuerySet []>
// --- 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
答案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('self',blank=True,related_name='user_following',symmetrical=False)
following = models.ManyToManyField('self',blank=True,related_name='user_followers',symmetrical=False)
(remember to migrate those changes to the database)
I would be curious what the behavior is then, please let me know
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论