NIKHIL RANE
NIKHIL RANE

Reputation: 4092

Django: Created django user that are extended with my django model field

Here it's my django model

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

now I want to register user that are extend by my model fields. here its my student register code

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()

here something wrong

I want to take json as a input and create user using above model field.

Upvotes: 0

Views: 31

Answers (2)

Prateek099
Prateek099

Reputation: 559

The code should be:

    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")

Now the user will be saved when password = password1, if that what you want.

Upvotes: 0

ignacio.munizaga
ignacio.munizaga

Reputation: 1603

I seems that you only save the user when the passwords don't match, after the return. Move the "user.save()" line one tab to the left.

Upvotes: 2

Related Questions