If you want to extend the Django User model, first check this article. It shows different ways of extending the User model. In my last workaround I needed all the information in Neo4j so I adapt my model to have the fields of the user in my model (It was a model of Student). Whenever a new student register to the app, I have a signal to react after the save (post_save) and it stores the password and the username. You can explore the Django signals here
For the model I have:
class StudentProfile(DjangoNode):
first_name = StringProperty(max_length=30)
last_name = StringProperty(max_length=150)
email = EmailProperty()
birth = DateProperty()
username = StringProperty(max_length=150, unique=True)
password = ''
For the signal:
@receiver(post_save, sender=StudentProfile, dispatch_uid='create_user_student')
def create_user_student(sender, instance, created, **kwargs):
if created:
user = User.objects.create_user(instance.username)
user.set_password(instance.password)
user.save()
@receiver(post_delete, sender=StudentProfile, dispatch_uid='delete_user_student')
def delete_user_student(sender, instance, **kwargs):
User.objects.filter(username=instance.username).delete()
Besides the main view of the StudentProfile, I have a view that uses the built-in Django authentication system:
from django.contrib.auth import authenticate, login as do_login, logout as do_logout
...
@api_view(["POST"])
def login(request):
username = request.data.get('username')
password = request.data.get('password')
user = authenticate(username=username, password=password)
if user is not None:
do_login(request, user)
return Response({'login': 'ok'}, status=status.HTTP_200_OK)
return Response({'login': 'Error on credentials'}, status=status.HTTP_403_FORBIDDEN)