0

I am using django social auth for social network login into my site and it works fine the user info is stored in the User model. But i have another model(ExtraInfo) that inherits the user model to store extra data. How do i create an new instance in the sub-model(ExtraInfo) when a new user is created in the User model by django social auth

I tried SOCIAL_AUTH_USER_MODEL = 'myapp.CustomUser' but this does not work since create_user method is not available for the sub-model

class ExtraInfo(User):
      phoneNo = models.CharField(max_length = 30)
      city = models.CharField(max_length = 30)
      state = models.CharField(max_length = 30)
Troy Matt
  • 35
  • 5

2 Answers2

1

Depends on your project, but it looks like your ExtraInfo model shouldn't inherit from User model but instead extend it as mentioned by @miki725, or by using django profiles.

Regarding django-social-auth, you can extend the default pipeline with a function that creates the needed ExtraInfo model (you can check if the user is new with the is_new argument).

If users are created by other means, then signal is the way to go.

omab
  • 3,721
  • 19
  • 23
0

As per Django docs, the recommended way to add attributes for a User is to use OneToOneField instead of subclassing like:

class ExtraInfo(models.Model):
    user = models.OneToOneField(User, related_name='extra_info')
    phoneNo = models.CharField(max_length = 30)
    city = models.CharField(max_length = 30)
    state = models.CharField(max_length = 30)

As for your problem, you can connect to Django's post_save signal (docs) and then whenever created parameter will be True, then create an ExtraInfo model for that new user.

miki725
  • 27,207
  • 17
  • 105
  • 121