Questions tagged [django-polymorphic]

An app to help with Django model inheritance

When a django model is subclassed, queries involving the parent model return instances of the parent model. However when django-polymorphic is used, queries with the parent model return instances of the subclasses where applicable.

more information at: https://django-polymorphic.readthedocs.io/en/stable/

51 questions
11
votes
2 answers

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…
mastachimp
  • 438
  • 3
  • 14
6
votes
1 answer

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…
lukaszzenko
  • 317
  • 4
  • 11
5
votes
2 answers

Filtering plain models on their relation to a subclass of a django polymorphic model?

I have a plain Django model that has a ForeignKey relation to a django-polymorphic model. Let's call the first PlainModel that has a content ForeignKey field to a polymorphic Content model with subtypes Video and Audio (simplified example). Now I…
Bartvds
  • 3,340
  • 5
  • 30
  • 42
5
votes
1 answer

How to exclude fields from form created via PolymorphicChildModelAdmin

Playing a little around with Polymorphic and additional plugins I'm wondering how I can prevent some of the base class fields from being showed inside form for child admin interface. Having this adminy.py for my child class: from django.contrib…
frlan
  • 6,950
  • 3
  • 31
  • 72
4
votes
1 answer

Django-PolymorphicModels - Error when deleting an instance

It's the first time I'm trying 'PolymorphicModel', maybe there is something I'm doing wrong or I'm trying to do something that is not supported. Is there anyway to get around this? My simplified model: from django.polymorphic import…
3
votes
0 answers

How prevent creating a base model in django-polymorphic

I want to only be able to create new objects on sub-classes and not on base class. class Product(PolymorphicModel): # fields class ProductType1(Product): # fields class ProductType2(Product): # fields For example in above code…
3
votes
0 answers

Python Inheritance with unique attribute in master class

I'm with a little problem. class Person(models.Model): cpf = BRCPFField(**unique=true**) class Student(Person): new_field = .... class Teacher(Student): another_field = ... class BoardMember(Person): class Meta: proxy =…
3
votes
0 answers

Empty Queryset when using a Polymorphic Model from django-polymorphic and querying against the proxy model

I am using the django-polymorphic package: https://pypi.org/project/django_polymorphic/ I was looking at the documentation for Proxy Models in the docs: https://docs.djangoproject.com/en/1.11/topics/db/models/#proxy-models Initial Setup - I tried…
Aero Chocolate
  • 1,477
  • 6
  • 23
  • 39
3
votes
1 answer

How do I filter based on instance type when using Django-Polymorphic package?

I'm a bit stuck on having to create a Django query to filter based on instance type when using django-polymorphic. from polymorphic.models import PolymorphicModel class ClassA(models.Model): project = select2.fields.ForeignKey(Project,…
Aero Chocolate
  • 1,477
  • 6
  • 23
  • 39
3
votes
0 answers

Changing "choices" for children classes in Django-Polymorphic package

Is it possible to use django-polymorphic package and have a CharField in the parent class and change the "choices" depending on the children classes? Ex) class Shape(PolymorphicModel): name = models.CharField(max_length=255) status =…
Aero Chocolate
  • 1,477
  • 6
  • 23
  • 39
3
votes
0 answers

Django-polymorphic pre-save signal

I'm trying to switch to django-polymorphic models in my ecommerce project, but found problem with pre-save signals. When creating new product via Admin I need to fill 'Slug' field of my object with unique value. In non polymorphic version it was…
3
votes
1 answer

Django Rest Framework: Derived model serializer fields

I'm working on building a tree-like hierarchical database system using Django Rest Framework and django-polymorphic-tree. I have two models- BaseTreeNode and DescriptionNode (the later one is derived from BaseTreeNode). Specifically, here's my…
3
votes
1 answer

django-polymorphic-tree serializer

I would like to serialize all the nodes in my PolymorphicMPTTModel with their corresponding fields. Following the documentation django-polymorphic and django-mptt i get this: { "count":1, "next":null, "previous":null, "results":[ …
3
votes
4 answers

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…
toscanelli
  • 1,202
  • 2
  • 17
  • 40
2
votes
0 answers

Foreign key of foreign key in django-polymorphic

I have the following (simplified) django and django-polymorphic models: class GenericOffer(PolymorphicModel): pass class OfferA(GenericOffer): special = models.IntegerField() class OfferB(GenericOffer): pass class…
Pierre GM
  • 19,809
  • 3
  • 56
  • 67
1
2 3 4