zambotn
zambotn

Reputation: 785

Django models complex inheritance with proxys

I have these models:

class Base(models.Model):
    # ... attributes
    def f(self):
        raise Exception()

class A(Base):
    attribute = models.IntegerField()

class B(A):
    class Meta:
        proxy = True
    def f(self):
        print "B", attribute

class C(A):
    class Meta:
        proxy = True
    def f(self):
        print "C", attribute

Now i play a bit with them, but i found a problem:

c = C()
c.attribute = 1
c.save()

b = Base.objects.all()
b.f() # Aspected "C 1" to be printed, exception fired instead!

The last line work in unexpected way! So looking to the documentation better, I searched for the Django automatic downcasting attribute, but i found only the one of the A() class that haven't automatic casting for neither B() or C().

There's a way to save the inheritance also when i save the data? Thanks!

Upvotes: 0

Views: 120

Answers (1)

Mikael
Mikael

Reputation: 3236

As per documentation, a Queryset still returns the model that was requested. See here

In your example you return a Queryset for the original model, Base.

Upvotes: 1

Related Questions