django - 覆盖 Django 中继承模型的字段选项
问题描述
我发现了类似的问题和答案,但似乎没有一个是完全正确的。我有一个像这样的抽象基础模型:
class BaseModel(models.Model):
timestamp = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
description = models.CharField(max_length=512, blank=True, null=True, help_text='Help')
class Meta:
abstract = True
然后我继承它:
class AnotherModel(BaseModel):
field1 = models.CharField(max_length=512)
但我希望这个模型help_text
在description
球场上是别的东西,比如help_text='Some other help text'
最好的方法是什么?我可以覆盖继承模型中的字段选项吗?
解决方案
如果这真的是关于帮助文本,我建议只覆盖 ModelForm。但是,您可以使用工厂并返回内部类:
def base_factory(description_help: str = "Standard text"):
class BaseModel(models.Model):
timestamp = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
description = models.CharField(
max_length=512, blank=True, null=True, help_text=description_help
)
class Meta:
abstract = True
return BaseModel
class ConcreteModel(base_factory("Concrete help")):
field1 = ...
推荐阅读
- delphi - 如何在 TcxGrid 上显示自定义记录数
- r - 如何在R中的数据框中相互比较行
- go - Golang 迭代嵌套映射并将键更改为标题大小写
- python - 如何修复 Porter Stemmer 错误?
- flutter - Flutter:使用 GPS 确定车辆的速度
- python - 安装 Python http 包时遇到问题
- git - NGINX fastcgi 代理上使用 git-http-backend 的多个同时请求
- jquery - 在移动 jQuery 上隐藏地址和浏览器页脚
- azure - 可以通过 Web 应用程序防火墙状态/计费以编程方式暂停/恢复 Azure 的应用程序网关吗?
- python - python numpy linalg求解器:错误答案