Hey, everyone! I have seen a lot of posts on Stack Overflow regrading bulky models.py files so I thought I could post my solution to the problem. The docs explain how to separate each model class into its own file if need be so I will not get into that. This post is centered around model methods and how they can quickly increase file size depending on their usage.
My solution: create an inheritance hierarchy.
-
Create an abstract model to hold the model fields
ex: class UserAbstract(models.Model):
field_one…
field_two…
class Meta:
abstract = True -
Create another abstract model to hold model methods that inherits from the model in #1. Method implementations can be placed in this file due to the model fields being defined in the UserAbstract class.
ex: UserImpl(UserAbstract):
method 1…
method 2…
class Meta:
abstract = True -
Create the final model that inherits from the model in #2 and define your official meta class attributes.
ex: class User(UserImpl):
class Meta:
…
Not everyone will need to use this implementation, but it serves as a great way to organize your code if your model files start to get really big. Let me know what you think!