我想找到一种创建模型适配器的方法,我会做更好的解释,
class Question (models.Model):
question_text = models.CharField (max_length = 200)
pub_date = models.DateTimeField ('date published')
def __str __ (self):
return self.question_text
def was_published_recently (self):
return self.pub_date> = timezone.now () - datetime.timedelta (days = 1)
class Choice (models.Model):
question = models.ForeignKey (Question, on_delete = models.CASCADE)
choice_text = models.CharField (max_length = 200)
votes = models.IntegerField (default = 0)
def __str __ (self):
return self.choice_text
我不想从models.Model继承,但是我想要我的东西,可以管理和更改结构,而不必对这些代码发疯。 另一种可能性是与Decorator一起使用,或者将它们视为Java es中的JPA:
@Entity
class Question:
@CharField (max_length = 200)
QUESTION_TEXT
也就是说,在实践中,我不会选择model.Model,但是可以随时互换自己的东西