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 user.
class User(models.Model):
name = models.CharField(max_length=200)
class FavouriteList(models.Model):
user = models.OneToOneField(User)
favourites = models.ManyToManyField(Favourite, blank=True)
When a new user is created, I want to ensure that the user has a FavouriteList
. I've looked around in the Django documentation and haven't had much luck.
Does anyone know how I can ensure that a model has a child object (e.g. FavouriteList
) when it is created?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…