使用元类动态扩展 Django 模型

Dynamically extending django models using a metaclass

问题

我正在尝试构建一个自动扩展模型 classes 具有额外字段的框架。以下是我正在尝试做的事情的简短摘要:

给定一个模型class

class Pizza(models.Model):
    name = models.CharField(max_length=10)
    price = models.DecimalField(max_digits=10, decimal_places=2)

我想自动生成一个 class,每个 class 字段都有一个附加字段,生成一个 class 类似于以下内容:

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-Fields

的方法

我的方法

上面扩展 Pizza class 的例子可以用下面的代码解决:

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 class 已由 EntityMetaclass 元 class 成功扩展。

问题所在

不幸的是,当模型包含 ForeignKey-Field 时,上述代码失败,产生以下回溯:

  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.

有办法解决这个问题吗?

棘手!

问题在于,当您尝试 .clone 模型中的字段时,它们尚未完全初始化,因此,Django 机制通过其字符串 qualifeid 名称查找 foreign-model无法使用。问题是代码不检查外国模型是否作为 class 引用而不是字符串传递。

解决此问题的唯一方法似乎是 monkey-patching 在创建 classes 时进行这些检查。 同时,在克隆 foreign-object 字段时,related_field - 由 django ORM 自动创建的反向引用,因此可以从 "pointed-at" 对象获取 "holder" 对象必须显式传递给新的、克隆的字段。否则它会指向原始字段。 这需要更多的 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 class 上的结果字段:

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>)