天道酬勤,学无止境

django-polymorphic

使用 ABC、PolymorphicModel、django-models 产生元类冲突(Using ABC, PolymorphicModel, django-models gives metaclass conflict)

问题 到目前为止,关于 SO 的所有其他答案都以完全相同的方式回答:构建您的元类,然后继承这些元类的“加入”版本,即 class M_A(type): pass class M_B(type): pass class A(metaclass=M_A): pass class B(metaclass=M_B): pass class M_C(M_A, M_B): pass class C:(A, B, metaclass=M_C): pass 但我不知道这些人生活在什么世界,他们在那里构建自己的元类! 显然,人们会使用来自其他库的类,除非您对元编程有完美的掌握,否则您怎么知道是否可以覆盖类的元类? (显然我还没有处理它们)。 我的问题是: class InterfaceToTransactions(ABC): def account(self): return None ... class Category(PolymorphicModel, InterfaceToTransactions): def account(self): return self.source_account ... class Income(TimeStampedModel, InterfaceToTransactions): def account(self): return self

2021-10-20 06:06:37    分类:技术分享    python   django   metaclass   abc   django-polymorphic

更改 django 模型实例的多态内容类型(Change the polymorphic content type of a django model instance)

问题 如果我有一个多态模型: class Father(polymorphic.model.PolymorphicModel) 和一个没有额外字段的继承类: class Child(Father) 当我有一个父亲实例时,如何将其转换为子实例? 我尝试过的是: foo = Father.objects.get(pk=1) # foo is just a Father, no record in Child database table. foo.polymorphic_ctype = ContentType.objects.get(app_label='myapp', model='child') foo.save() 但什么都没有改变。 我希望foo是一个 Child 对象,并且需要将它放入子数据库表中。 回答1 好的。 在阅读了这篇类似的文章后,我意识到我需要另一个任务: foo.__class__ = Child 这会在子表中插入,否则永远不会发生。 回答2 Django 会自动将条目复制到父项中的子项中。 对于创建 Child 类的迁移,您可以简单地在数据库中保存新的 Child 对象(使用 python 对象的__dict__属性填充它们): 在迁移文件中定义此函数: def duplicate_Children(apps,schema_editor): Father =

2021-10-08 08:05:02    分类:技术分享    django   django-contenttypes   django-polymorphic

django-polymorphic 按子类型过滤(django-polymorphic Filter by child type)

问题 我有如下模型结构: class MyObject(PolymorphicModel): group = models.ForeignKey(Group) class Group(PolymorphicModel): pass class SpecialGroup(Group): pass 现在,我想选择所有 MyObjects,哪个组是 SpecialGroup 类型的。 是否可以使用 QuerySet API 实现它,而无需运行原始 SQL? 我想出的唯一可行的解​​决方案是使用 .extra() 运行额外的“选择”SQL 查询。 提前致谢,干杯! 回答1 在内部,django_polymorphic 使用 Django 的 ContentType 框架来确定用于每个模型的实际类。 from django.contrib.contenttypes.models import ContentType MyObject.objects.filter(group__polymorphic_ctype=ContentType.objects.get_for_model(SpecialGroup))

2021-08-30 10:45:47    分类:技术分享    django   polymorphism   django-polymorphic

Using ABC, PolymorphicModel, django-models gives metaclass conflict

So far every other answer on SO answers in the exact same way: construct your metaclasses and then inherit the 'joined' version of those metaclasses, i.e. class M_A(type): pass class M_B(type): pass class A(metaclass=M_A): pass class B(metaclass=M_B): pass class M_C(M_A, M_B): pass class C:(A, B, metaclass=M_C): pass But I don't know what world these people are living in, where they're constructing your own metaclasses! Obviously, one would be using classes from other libraries and unless you have a perfect handle on meta programming, how are you supposed to know whether you can just override

2021-07-11 23:08:43    分类:问答    python   django   metaclass   abc   django-polymorphic

Change the polymorphic content type of a django model instance

If I have a polymorphic model: class Father(polymorphic.model.PolymorphicModel) and an inheritor class with no extra fields: class Child(Father) When I have an instance of Father, how can I convert it to a Child instance? What I have tried is: foo = Father.objects.get(pk=1) # foo is just a Father, no record in Child database table. foo.polymorphic_ctype = ContentType.objects.get(app_label='myapp', model='child') foo.save() But nothing changes. I want foo to be a Child object and need to have this into the child database table.

2021-06-25 10:20:06    分类:问答    django   django-contenttypes   django-polymorphic

django-polymorphic Filter by child type

I have models structure like below: class MyObject(PolymorphicModel): group = models.ForeignKey(Group) class Group(PolymorphicModel): pass class SpecialGroup(Group): pass Now, I would like to select all MyObjects, which group is of type SpecialGroup. Is it possible to achieve it with QuerySet API, without running raw SQL? The only working solution I came up with was by running additional 'select' SQL query using .extra(). Thanks in advance, Cheers!

2021-06-24 07:57:01    分类:问答    django   polymorphism   django-polymorphic