Create OneToOne instance on model creation

前端 未结 3 2077
孤街浪徒
孤街浪徒 2020-12-13 14:50

I\'m building my first django app. I have a user, and the user has a list of favourites. A user has exactly one list of favourites, and that list belongs exclusively to that

3条回答
  •  一向
    一向 (楼主)
    2020-12-13 15:36

    One way would be to register a post_save signal with next handler:

    def user_save(sender, instance, created, **kwargs):
      if created:
         FavouriteList.objects.get_or_create(user=instance)
    

提交回复
热议问题