django: create user profile for existing users automatically

前端 未结 3 1356
终归单人心
终归单人心 2021-01-03 18:47

I added a new UserProfile Model to my project today.

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    ...

    def __unicode__(self         


        
3条回答
  •  太阳男子
    2021-01-03 19:30

    You can loop through the existing users, and call get_or_create():

    for user in User.objects.all():
        UserProfile.objects.get_or_create(user=user)
    

    You could put this in a data migration if you wish, or run the code in the shell.

提交回复
热议问题