[Fixed]-Django-carton work with two Product models

1👍

In your base model you can define method get_type without implementation, and in inherited classes, implement it, for example:

class Product(models.Model):
    ...
    def get_type(self):
        pass
    class Meta:
        ...
        abstract = True

class Pub(Product):
    ...
    def get_type(self):
        return 'pub'

class Restaurant(Product):
    ...
    def get_type(self):
        return 'restaurant'

If you know the type of an instance, you can easily access needed attributes.

Or you can use just isinstance built-in function:

if isinstance(obj, Pub):
    # Your logic here

Leave a comment