How to query abstract-class-based objects in Django? -


Assuming that I have the abstract element square that looks like this:

  class StellarObject: Title = Model.carfield (max_long = 255) Description = Model. TextField () Slug = Model. SloughField (empty = true, zero = true) square meta: abstract = true  

Now, suppose I have two actual database classes, which are obtained from StellarObject

Star Planet (StellarObject): type = models.CharField (max_length = 50) size = models.IntegerField (max_length = 10) Class star (Stellar object): mass = model.Intergrungfield (max_long = 10)

So far, it's great if I want to get the planet or stars, then I do all this:

  Thing.objects.all () # And Thing.objec Ts.filter () # or count (), etc. .  

But what if I have to get all the stellar objects? If I do:

  StellarObject.objects.all ()  

This returns an error, because an abstract class is not a real database object, And therefore can not be inquired. What I have read, says that I have to do one on each of the two questions, planets and stars, and then they have to merge. It seems extremely incomprehensible. Is this the only way?

On its root, this is part of the mismatch between objects and relational databases. The ORM does a great job in eliminating differences, but sometimes you come in any way against them.

In fact, you have to choose between a different heritage, in which case there is no database connection between classes, or multi-table succession, which costs the cost of each query (an additional Database is included) in the database.


Comments