0

I'm building a DRF app with Django-rest-auth implemented for social login. The problem I'm having now is that I want to extend my User Model, but I'm afraid if I change the User Auth Model migrations will complete destroy my project due to the rest-auth link.

So my option is to create a UserData field and hook it to User through a one to one field. However, I'm not sure how to instantiate a new UserData object and link it to the User every time a new user is sign up or created through Django-rest-auth api.

Any help and advice for this would be appreciated.

Sylph
  • 151
  • 1
  • 11
  • Possible duplicate of [Can Django automatically create a related one-to-one model?](https://stackoverflow.com/questions/1652550/can-django-automatically-create-a-related-one-to-one-model) – Dušan Maďar Aug 05 '18 at 10:05

1 Answers1

1

It looks like that django signal is your best way to achieve this.

You can simply register a receiver function which will be called everytime new user is created ( or model is updated ).

There you can create your UserData.

Example:

from django.db.models.signals import post_save
from django.dispatch import receiver

@receiver(post_save, sender=User)
def create_user_data(sender, update_fields, created, instance, **kwargs):
    if created:
        # Create your user data
        pass

created will be True if new instance has just been created. instance is the new user instance.

The sender is the model of your User ( not sure which one it is in case of django-rest-auth) - just make sure it is the correct one.

Enthusiast Martin
  • 3,041
  • 2
  • 12
  • 20