Home > Software design >  Could there be any issues naming a Django model "Model"?
Could there be any issues naming a Django model "Model"?

Time:11-09

I am looking through a new client's code and they named one of their Django models "Model." Could this cause any issues since all models are considered models or no?

CodePudding user response:

It is not a problem naming a class Model as long as that Model is not used as super class for other models. Indeed, one can define a:

from django.db import models

class Model(models.Model):
    # …

class WrongOtherModel(Model):
    # …

class CorrectOtherModel(models.Model):
    # …

Here the WrongOtherModel will inherit from the Model class defined in the file, which is likely not what we want to do: only if you want to use the defined Model as supercass, you use Model as superclass. If you want to define another model, you can do this by inheriting from models.Model.

That being said, I would advise not to name a model Model, since it makes it more likely that such mistakes will happen, but perhaps more importantly: it can result in a lot of confusion if you have to explain how the modeling works.

  • Related