django-polymorphic icon indicating copy to clipboard operation
django-polymorphic copied to clipboard

Somewhat effective way of using values()

Open rrauenza opened this issue 7 years ago • 2 comments

I wonder if you might want to document this alternative when using values()? Even though this isn't really django-polymorphic specific, I think you're the one adding in the ctype.

In this example, we have A->B->C and A->D->E

E has a column 'foo', and C has a column 'bar'.

This works by joining in the model name from the ctype. Then also joining in the specific attributes of the inherited models ...

A.objects.values('polymorphic_ctype__model', 'b__c__foo', 'd__e__bar')

One could look at the model string returned instead of isinstance and then select which fields to look at in the code. The irrelevant ones end up being None.

...maybe this is obvious, but it was kind of an Aha! moment for me when trying to convert some queries to values().

rrauenza avatar Feb 12 '18 21:02 rrauenza

Thanks a lot! This is actually not obvious at all

neoden avatar Aug 08 '23 07:08 neoden

@neoden My 5 years older self now doesn't remember why I added this -- I didn't state what problem I was solving!

rrauenza avatar Aug 17 '23 15:08 rrauenza