Create user profiles in Django if they does not exist
If you store additional user information in a custom profile model, possibly you have
post_save hook to create them during signup:
class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) # your fields @receiver(post_save, sender=User) def create_user_profile(sender, instance, created, **kwargs): if created: Profile.objects.create(user=instance)
But if you already have users in your database without profiles, possibly you need also create them. This will allow omitting redundant checks e.g.
To do this we can create migration like described in datamigration hint, with the next action:
def create_profiles(apps, schema_editor): User = apps.get_model(app_label='auth', model_name='User') Profile = apps.get_model("main", "Profile") for u in User.objects.all(): if not hasattr(u, 'profile'): Profile.objects.create(user=u) u.save() class Migration(migrations.Migration): dependencies = [ ('main', '0018_auto_20170408_1407'), ] operations = [ migrations.RunPython(create_profiles), ]