gpt4 book ai didi

python - 使用元类动态扩展 django 模型

转载 作者:行者123 更新时间:2023-12-01 07:38:20 25 4
gpt4 key购买 nike

问题

我正在尝试构建一个框架,该框架可以自动使用附加字段扩展模型类。以下是我正在尝试做的事情的简短摘要:

给定一个模型类

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

的方法

我的方法

上面扩展 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-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 通过字符串 qualifid 名称查找外部模型的机制不能使用。问题在于代码不会检查foreignmodel是否作为类引用而不是字符串传递。

解决这个问题的唯一方法似乎是在创建类时对这些检查进行猴子修补。与此同时,当克隆外部对象字段时,相关字段 - 由 django ORM 自动创建的反向引用,以便可以从“指向”对象到“持有者”对象,必须显式传递给新的克隆对象, field 。否则它将指向原始字段。这需要更多的猴子修补,以在 .clone 调用的内部工作中插入显式的“lated_name”参数。

这两件事已经完成,看起来很有效。这是我根据您使用的代码:

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

关于python - 使用元类动态扩展 django 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56858765/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com