本文介绍了南方是否处理模型混合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经在某些模型中创建了一个mixin并继承自它。问题是当我创建模式迁移时,mixin的字段在那里。 class MyMixin(object):
a_field = models.CharField(max_length = 30,blank = True)
another_field = models.DateTimeField(blank = True,null = True)
class Meta:
abstract = True
class MyModel .Model,myMixin):
...
任何想法?
解决方案
似乎已经使用以下
class MyMixin(models.Model):
a_field = models.CharField(max_length = 30,blank = True)
another_field = models.DateTimeField(blank = True,null = True)
class Meta:
abstract = True
class MyModel(myMixin,models.Model):
...
更改是:
- MyMixin继承模型而不是对象(尽管围绕这个地方的许多讨论说,django的mixins应该继承对象而不是模型) / li>
- MyModel的继承顺序 - mixin必须先到达
I've created a mixin and inherited from it in some models. The problem is when I create a schema migration, the mixin's fields are there.
class MyMixin(object):
a_field = models.CharField(max_length=30, blank=True)
another_field = models.DateTimeField(blank=True, null=True)
class Meta:
abstract = True
class MyModel(models.Model, myMixin):
...
Any ideas?
解决方案
Seem to have got it working using the following
class MyMixin(models.Model):
a_field = models.CharField(max_length=30, blank=True)
another_field = models.DateTimeField(blank=True, null=True)
class Meta:
abstract = True
class MyModel(myMixin, models.Model):
...
The changes are:
- MyMixin inherits Model rather than object (despite many discussions around the place saying that mixins for django should inherit object rather than Model)
- the order of inheritance for MyModel - the mixin has to come first
这篇关于南方是否处理模型混合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!