我有一个User
模型:
class User(models.Model):
SEX = (
('M', 'Male'),
('F', 'Female'),
)
name = models.CharField("Name", max_length=100, blank=False)
surname = models.CharField("Surname",max_length=150, blank=False)
is_real = models.BooleanField("Real user", default=False)
sex = models.CharField("Sex", max_length=1, choices=SEX)
age = models.IntegerField("Age", blank=False)
每个User
人都有一个电子邮件或一些:
class Email(models.Model):
address = models.EmailField("Email Address", max_length=250)
login = models.CharField("Login", max_length=50)
password = models.CharField("Password", max_length=15)
date_created = models.DateField("Registered", auto_now_add=True)
last_login = models.DateTimeField("Last login")
belongs_to = models.ForeignKey(User, verbose_name="Owner")
也User
可能有博客:
class Blog(models.Model):
address = models.URLField("Blog Url")
related_email = models.ForeignKey(Email, verbose_name="Registered with")
belongs_to = models.ForeignKey(User, blank=True)
博客通常通过电子邮件注册。所以我需要的是,如果我没有指定博客的所有者,它应该自动设置为注册电子邮件的所有者。我认为这可以通过压倒一切的save
方法来完成,但不确定如何处理ForeignKey
关系。