Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango: using ContentType vs multi_table_inheritance
    primarykey
    data
    text
    <p>I was having a similar problem as in <a href="https://stackoverflow.com/questions/3797982/how-to-query-abstract-class-based-objects-in-django">How to query abstract-class-based objects in Django?</a> The thread suggests using multi_table_inheritance. I personally think using content_type more conceptually comfortable (just feels more close to logic, at least to me)</p> <p>Using the example in the previous link, I would just add a StelarType as</p> <pre><code>class StellarType(models.Model): """ Use ContentType so we have a single access to all types """ content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() content_object = generic.GenericForeignKey('content_type', 'object_id') </code></pre> <p>Then add this to the abstract base model</p> <pre><code>class StellarObject(BaseModel): title = models.CharField(max_length=255) description = models.TextField() slug = models.SlugField(blank=True, null=True) stellartype = generic.GenericForeignKey(StellarType) class Meta: abstract = True </code></pre> <p>To sync between StellarObject and StellarType, we can connect post_save signal to create a StellarType instance every time a Planet or Star is created. In this way, I can query StellarObjects through StellarType. So I'd like to know what's the PRO and CON of using this approach against using multi_table_inheritance? I think both create an additional table in the databse. But how about database performance? how about usability/flexibility? Thanks for any of your input!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload