我正在尝试构建一个框架,该框架会自动扩展具有附加字段的模型类。这是我要做什么的简短摘要:
给出一个模型类
class Pizza(models.Model):
name = models.CharField(max_length=10)
price = models.DecimalField(max_digits=10, decimal_places=2)
我自动希望生成一个类,每个类字段具有一个附加字段,产生一个类似于以下内容的类:
class PizzaGenerated(models.Model):
name = models.CharField(max_length=10)
name_new = models.CharField(max_length=10)
price = models.DecimalField(max_digits=10, decimal_places=2)
price_new = models.DecimalField(max_digits=10, decimal_places=2)
如您所见,对于Pizza
的每个属性,都添加了带有后缀_new
的附加字段。
无论模型的结构如何,我都需要我的解决方案来工作。特别是,我正在寻找一种允许复制ForeignKey
-字段
上述扩展Pizza
类的示例可通过以下代码解决:
class ResMetaclass(models.base.ModelBase):
def __new__(cls, name, bases, attrs):
fields = {
k: v for k, v in attrs.items() if not k.startswith('_') and isinstance(v, models.Field)
}
attrs_extended = {
**attrs,
**{fieldname + '_new': fieldtype.clone() for fieldname, fieldtype in fields.items()}
}
bases = (models.Model,)
clsobj = super().__new__(cls, name, bases, attrs_extended)
return clsobj
class EntityBase(models.Model, metaclass=ResMetaclass):
class Meta:
abstract = True
class Pizza(EntityBase):
name = models.CharField(max_length=10)
price = models.DecimalField(max_digits=10, decimal_places=2)
Pizza
元类成功扩展了EntityMetaclass
类。
不幸的是,当模型包含一个ForeignKey
-字段时,上面的代码失败,并产生以下回溯:
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/home/niklas/dev/pyflx/venv/lib64/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/home/niklas/dev/pyflx/venv/lib64/python3.7/site-packages/django/core/management/__init__.py", line 357, in execute
django.setup()
File "/home/niklas/dev/pyflx/venv/lib64/python3.7/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/niklas/dev/pyflx/venv/lib64/python3.7/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
File "/home/niklas/dev/pyflx/venv/lib64/python3.7/site-packages/django/apps/config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib64/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/niklas/dev/pyflx/mig/models.py", line 20, in <module>
class Contact(EntityBase):
File "/home/niklas/dev/pyflx/pyflx/models.py", line 17, in __new__
**{fieldname + '_patch_value': fieldtype.clone() for fieldname, fieldtype in fields.items() if fieldname != 'id'},
File "/home/niklas/dev/pyflx/pyflx/models.py", line 17, in <dictcomp>
**{fieldname + '_patch_value': fieldtype.clone() for fieldname, fieldtype in fields.items() if fieldname != 'id'},
File "/home/niklas/dev/pyflx/venv/lib64/python3.7/site-packages/django/db/models/fields/__init__.py", line 492, in clone
name, path, args, kwargs = self.deconstruct()
File "/home/niklas/dev/pyflx/venv/lib64/python3.7/site-packages/django/db/models/fields/related.py", line 856, in deconstruct
name, path, args, kwargs = super().deconstruct()
File "/home/niklas/dev/pyflx/venv/lib64/python3.7/site-packages/django/db/models/fields/related.py", line 583, in deconstruct
swappable_setting = self.swappable_setting
File "/home/niklas/dev/pyflx/venv/lib64/python3.7/site-packages/django/db/models/fields/related.py", line 374, in swappable_setting
return apps.get_swappable_settings_name(to_string)
File "/home/niklas/dev/pyflx/venv/lib64/python3.7/site-packages/django/apps/registry.py", line 288, in get_swappable_settings_name
for model in self.get_models(include_swapped=True):
File "/home/niklas/dev/pyflx/venv/lib64/python3.7/site-packages/django/apps/registry.py", line 178, in get_models
self.check_models_ready()
File "/home/niklas/dev/pyflx/venv/lib64/python3.7/site-packages/django/apps/registry.py", line 140, in check_models_ready
raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
有没有办法解决这个问题?
答案 0 :(得分:1)
整rick!
问题在于,当您尝试对模型中的字段进行.clone
时,它们尚未完全初始化,因此,通过字符串qualifeid名称查找外部模型的Django机制无法实现用过的。问题在于代码不会检查是否将foreignmodel作为类引用而不是字符串来传递。
解决此问题的唯一方法似乎是在创建类时用猴子修补这些检查。
那时,克隆外部对象字段时,related_field-由django ORM自动创建的反向引用,因此可以从“指向”对象获取到“持有人”对象,必须显式传递给新的克隆对象。 ,字段。否则它将指向原始字段。
这需要更多的monkeypatching,才能在.clone
调用的内部工作中插入一个显式的“ related_name”参数。
完成这2件事,似乎可行。这是我根据您使用的代码:
from django.db import models
from django.db.models.fields import related
from unittest.mock import patch
class ResMetaclass(models.base.ModelBase):
def __new__(cls, name, bases, attrs):
fields = {
k: v for k, v in attrs.items() if not k.startswith('_') and isinstance(v, models.Field)
}
new_fields = {}
for field_name, field in fields.items():
new_field_name = field_name + "_new"
if not isinstance(field, related.RelatedField):
new_fields[new_field_name] = field.clone()
else:
real_deconstruct = field.deconstruct
def _deconstruct():
name, path, args, kwargs = real_deconstruct()
kwargs["related_name"] = new_field_name
return name, path, args, kwargs
with patch("django.apps.registry.apps.check_models_ready", lambda: True):
field.deconstruct = _deconstruct
# Assume foregnKeys are always within the same file, and
# disable model-ready checking:
new_fields[new_field_name] = field.clone()
del field.deconstruct
attrs_extended = {
**attrs,
**new_fields
}
bases = (models.Model,)
clsobj = super().__new__(cls, name, bases, attrs_extended)
return clsobj
class EntityBase(models.Model, metaclass=ResMetaclass):
class Meta:
abstract = True
class Pizza(EntityBase):
name = models.CharField(max_length=10)
price = models.DecimalField(max_digits=10, decimal_places=2)
class MenuEntry(EntityBase):
entry_number = models.IntegerField()
pizza = models.ForeignKey("Pizza", on_delete="cascade")
以及MenuEntry
类上的结果字段:
In [1]: from test1.models import Pizza, MenuEntry
In [2]: MenuEntry._meta.fields
Out[2]:
(<django.db.models.fields.AutoField: id>,
<django.db.models.fields.IntegerField: entry_number>,
<django.db.models.fields.related.ForeignKey: pizza>,
<django.db.models.fields.IntegerField: entry_number_new>,
<django.db.models.fields.related.ForeignKey: pizza_new>)