我正在写一个mixin,它可以让我的模型很容易地被转换成一个深层的值(有点像.values(),但是遍历关系).这些定义最清晰的地方似乎是模型本身,a la:
class Person(models.Model, DeepValues):
name = models.CharField(blank=True, max_length=100)
tribe = models.ForeignKey('Tribes')
class Meta:
schema = {
'name' : str,
'tribe' : {
'name' : str
}
}
Person.objects.all().deep_values() => {
'name' : 'Andrey Fedorov',
'tribe' : {
'name' : 'Mohicans'
}
}
然而,Django抱怨我在class Meta
人中加入了这一点:
TypeError: 'class Meta' got invalid attribute(s): schema
(整个堆栈跟踪here)
现在,我想我可以在我的Mixin中精心地覆盖这一点,但是有没有一种更优雅的方式来存储这些信息呢?