【发布时间】:2017-05-22 22:12:39
【问题描述】:
我正在建立一个社交网络,用户应该可以在其中互相关注。所以我用一个字段定义了一个类用户:ManyToMany 来存储关注这个用户的用户。这就是我在 model.py 中所做的:
followings = models.ManyToManyField('self', blank=True)
这是我的 view.py:
@login_required
def follow_test(request):
name = request.POST.get('name', '')
user_followed = Dater.objects.get(username=name)
current_user = Dater.objects.get(id=request.user.id)
print current_user.followings # display my_app.Dater.None
current_user.followings.add(user_followed)
print current_user.followings # display my_app.Dater.None
我正确检索了我的用户(当前(关注某人)和关注的用户),但我无法将关注的用户添加到当前用户的关注集中。你能在我的视野中看到我做得不对的地方吗?
【问题讨论】:
-
你必须保存数据,添加你的模型代码。
-
我已经完成了:current_user.save() 但没有解决问题
-
不,不需要保存。不过,您还没有真正告诉我们您遇到了什么问题。
标签: python django database sqlite