лучший подход - использовать signals
в models.py
class Profile(models.Model):
user = models.OneToOneField(User,
primary_key=True,
on_delete=models.CASCADE,
help_text=_('User (Required).'),
)
[..]
создать отдельный signals.py
файл
from django.dispatch import receiver
from django.db.models.signals import pre_save, post_save
from .models import User, Profile
@receiver(post_save, sender=User, dispatch_uid='user.create_user_profile')
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User, dispatch_uid='user.save_user_profile')
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
затем зарегистрировать signals
в apps.py
(так как мы использовали @receiver
декоратор)
from django.apps import AppConfig
from django.utils.translation import ugettext_lazy as _
class AccountsConfig(AppConfig):
name = 'apps.accounts'
verbose_name = _('Accounts and Profiles')
def ready(self):
from . import signals
посмотрите это руководство https://simpleisbetterthancomplex.com/tutorial/2016/07/28/how-to-create-django-signals.html