[Answer]-Having one simple model for generic information and other that plug more fields in it

1đź‘Ť

âś…

Have you looked into model inheritance?

If not, its probably time you give it a read. https://docs.djangoproject.com/en/dev/topics/db/models/#model-inheritance

They way it works at the implementation level is, you define a “base” class which contains all the base details.

A very simplified example might be something like this

class Item(models.Model):
    name = models.CharField()
    price = models.CharField()
    location = models.CharField()
    catgegory = models.CharField()

    class Meta:
        abstract = True

class Computer(Item):
    CPU = models.CharField()
    RAM = models.CharField()
    HDD = models.CharField()

The abstract = True line indicates that Django shouldn’t make a database table for it. Its a generic base class for other classes to extend.

Because the Computer class inherits from the Item class, it has access to all its attributes . You can even extend the computer class into two Desktop and Laptop classes.

If you don’t already know about inheritance, I strongly suggest reading more about it. Wikipedia is always a good place to start 🙂

http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)

👤Ze'ev G

Leave a comment