Django:创建了用我的django模型字段扩展的django用户

时间:2015-02-18 11:58:29

标签: python django

这是我的django模型

class student(User):
    name = models.CharField(max_length = 200)
    phone_no = models.BigIntegerField()
    email_id = models.EmailField()
    version = models.IntegerField()

现在我想注册由我的模型字段扩展的用户。 这是我的学生注册码

def registerStudent(request):
    print request.body
    if request.body:
        dataDictionary = json.loads(request.body)
        username = dataDictionary['username']
        first_name = dataDictionary['first_name']
        last_name = dataDictionary['last_name']
        email = dataDictionary['email']
        password = dataDictionary['password']
        password1 = dataDictionary['password1']

        user=User()
        user.username = username
        user.first_name = first_name
        user.last_name = last_name
        user.email = email
        if password == password1:
            user.set_password(password)
        else:
            return HttpResponse(json.dumps([{"validation": "Password does not match", "status": False}]), content_type="application/json")
            user.save()

这里出错了

我想将json作为输入并使用上面的模型字段创建用户。

2 个答案:

答案 0 :(得分:2)

我似乎只在密码不匹配时才保存用户,返回后。将“user.save()”行向左移动一个选项卡。

答案 1 :(得分:0)

代码应为:

    if password == password1:
        user.set_password(password)
        user.save()
    else:
        return HttpResponse(json.dumps([{"validation": "Password does not match"
                                            ,"status": False}])
                            ,content_type="application/json")

现在,当密码=密码1时,用户将被保存,如果你想要的话。