简体   繁体   English

在ModelForm中设置主键

[英]Setting Primary key in ModelForm

This is my code to load EditScreen for User Model. 这是我的代码,用于为用户模型加载EditScreen。

@login_required ()            
def getUser (request, userId):

    if request.method  == "GET":
        my_record = User.objects.get(user_id=userId)
        form = UserUpdateForm (instance=my_record)

        print "Registration page loading..."

        return render(request, "EN/updateuser.html", {'form': form } )

But 'user_id' (PrimaryKey) is not going as part of 'form' when I get request for update User I'm not able to get the object instance to update it. 但是,当我收到更新用户请求时,“ user_id”(PrimaryKey)不会作为“表单”的一部分。我无法获取对象实例来对其进行更新。 How to set it along with the form so that I should be able to read it. 如何与表格一起设置它,以便我应该能够阅读它。 I thought of setting it through Hidden Value but I felt that is not a safe way. 我曾想通过“隐藏价值”进行设置,但我认为这不是一种安全的方法。

Please suggest. 请提出建议。

Why do you want to send it as part of the form? 为什么要将其作为表格的一部分发送? You have it in the URL already (and therefore as the userId argument to the view), there's no need to get it from anywhere else. 您已经在URL中拥有了它(因此作为视图的userId参数),因此无需从其他任何地方获取它。

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

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