S
S
stayHARD2015-07-06 17:49:33
Django
stayHARD, 2015-07-06 17:49:33

Why does not save users?

Why does not want to create a user?
models.py

class UserProfile(User):
  user = models.OneToOneField(User)
  notification = models.CharField(max_length=500)

views.py
@api_view(['POST'])
def register_user(request):
  """
  Example json:
  {"username":"vaska", "password":"qwerty", "email":"[email protected]", "first_name":"Vasya", "last_name":"Pupkin", "notification":"qweasdsdgfdfhdfhdfgdfhdfhffhdfh"}
  """
  if request.method == 'POST':
    if User.objects.filter(username = request.data['username']).exists():
      return Response({'status':'User with this username already exist!'})
    else:	
      if User.objects.filter(email = request.data['email']).exists():	
        return Response({'status':'User with this email already exist!'})
      else:
        new_user = User.objects.create_user(username=request.data['username'], password=request.data['password'], email = request.data['email'], first_name = request.data['first_name'], last_name = request.data['last_name'])
        print request.data['notification']
        profile = UserProfile(user=new_user, notification = request.data['notification'])
        print profile
        profile.save()
        return Response({'status':'ok'})

Error:
UNIQUE constraint failed: auth_user.username

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vov Vov, 2015-07-06
@stayHARD

You already have a user with the same name in the database.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question