假设我有以下模型和字段:
class User(AbstractBaseUser):
name_title
name_first
name_middle_initial
name_last
address_1
address_2
address_city
address_state
address_post_code
class Order(models.Model):
name_title
name_first
name_middle_initial
name_last
address_1
address_2
address_city
address_state
address_post_code
class Shipment(models.Model):
address_1
address_2
address_city
address_state
address_post_code
我们也可以说,这些模型都不是必然相关的-订单不必属于用户,货件也不必属于订单,等等。
我希望所有重复字段都相同-具有相同的verbose_name,validations,max_length等。
我尝试采用混合方法:
class AddressFieldsMixin(models.Model):
address_1
address_2
address_city
address_state
address_post_code
class NameFieldsMixin(models.Model):
name_title
name_first
name_middle_initial
name_last
class User(AbstractBaseUser, AddressFieldsMixin, NameFieldsMixin):
pass
class Order(models.Model, AddressFieldsMixin, NameFieldsMixin):
pass
class Shipment(models.Model, AddressFieldsMixin):
pass
...但是,如果我的Mixin
类继承自models.Model
,则会导致模型/继承冲突;否则,将导致“未知字段”错误。
在多个模型中重复使用“名称”字段和“地址”字段的正确方法是什么?谢谢!
答案 0 :(得分:1)
您应该制作模型abstract = True
[Django-doc]。喜欢:
class AddressFieldsModel(models.Model):
address_1 = ...
address_2 = ...
address_city = ...
address_state = ...
address_post_code = ...
class Meta:
abstract = True
class NameFieldsModel(models.Model):
name_title = ...
name_first = ...
name_middle_initial = ...
name_last = ...
class Meta:
abstract = True
class User(AddressFieldsModel, NameFieldsModel, AbstractBaseUser):
pass
class Order(AddressFieldsModel, NameFieldsModel, models.Model):
pass
class Shipment(AddressFieldsModel, models.Model):
pass
由于这些模型也继承自models.Model
,因此它们不是mixins [wiki],或者至少不是非常严格的定义。此外,由于这些类是从models.Model
继承的,因此应将这些类放在第一位,如果将models.Model
指定为第一祖先,则会导致MRO冲突。
如文档所述,抽象模型旨在简化字段和行为的继承:
当您要将一些常见信息放入许多其他模型中时,抽象基类很有用。您编写基类,并将
abstract=True
放入Meta
类。这样,该模型将不会用于创建任何数据库表。相反,当将其用作其他模型的基类时,会将其字段添加到子类的字段中。