简体   繁体   English

将对象主键从其视图传递到其ManyToMany-relationship对象视图

[英]Passing objects primary key from its view to its ManyToMany-relationship object view

I don't know if the title is clear. 我不知道标题是否明确。 It's quite difficult for me to explain it in simple way because the problem is not that simple and I'm not a very good English speaker. 我很难用简单的方式来解释它,因为问题并不那么简单,而且我的英语说得也不好。 Every suggestion for improving a title are welcome. 欢迎提出改进标题的任何建议。

So, let's go to the actual problem... In my application people can join different groups. 因此,让我们来看一个实际的问题...在我的应用程序中,人们可以加入不同的组。 Currently, I'm creating invitation system for this purpose, so user can send an invitation to another user. 目前,我正在为此目的创建邀请系统,以便用户可以向另一个用户发送邀请。 In a group view, I have a list of users which are not connected with this group, which allowing group members to invite those people. 在小组视图中,我有一个与此小组无关的用户列表,允许小组成员邀请这些人。 So, when I'm going to the group view, I am passing it's Primary Key. 因此,当我进入组视图时,我将其传递为主键。 To create database cell about the invitation I need a group's PK and users PK as well (I want to do it using another view, but I'm not sure it's the best solution). 要创建有关邀请的数据库单元,我还需要一个组的PK和用户PK(我想使用其他视图来做,但是我不确定这是最好的解决方案)。

My question is: How can I pass those two PKs to the new view where I will create group-user relation cell in a database? 我的问题是: 如何将这两个PK传递到新视图中,以便在数据库中创建组用户关系单元?

models.py : models.py

class Group(models.Model):
    name = models.CharField(max_length=500)
    members = models.ManyToManyField(User, blank=True, related_name="member")
    invitations = models.ManyToManyField(User, blank=True, related_name="invitations")

views.py : views.py

def not_yet_members_list_view(request, pk):
    group = Group.objects.get(pk=pk)

    not_yet_members = User.objects.all() #doesn't matter

    args = {'group': group, 'not_yet_members': not_yet_members}
    return render(request, 'groups/show_members.html', args)

# Here is my problem (this code is a simplified version of what I want to achieve)
def invite_user_view(request, group_pk, user_pk):
    invite_user(group_pk, user_pk)

    return render(request, 'groups/show_members.html')

urls.py : urls.py

url(r'^not_yet_members_list/(?P<pk>\d+)/$', views.not_yet_members_list, name='not_yet_members_list'),

template.html : template.html

{% for user in not_yet_members %}
    <!-- ??? -->{{ user }}<a href="{% url 'groups:invite_user_view' pk=group.id %}">Invite</a>
{% endfor %}

in the view from where you need to pass the group and user 在需要传递组和用户的视图中

request.session['group_pk'] = group_pk
request.session['user_pk']= user_pk

in the view where you need to access the data 在需要访问数据的视图中

group_pk =  request.session['group_pk']
user_pk = request.session['user_pk']

then 然后

invite_user(group_pk, user_pk)

after successful sending invitation 成功发送邀请后

del request.session['group_pk']
del request.session['user_pk']
request.session.modified = True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM