django-polymorphic

Change the polymorphic content type of a django model instance

送分小仙女□ 提交于 2020-01-15 09:58:08
问题 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

Using ABC, PolymorphicModel, django-models gives metaclass conflict

时光总嘲笑我的痴心妄想 提交于 2019-12-23 09:36:26
问题 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

django-polymorphic Filter by child type

旧街凉风 提交于 2019-12-23 05:30:07
问题 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! 回答1: Internally, django_polymorphic uses