0

If each of Model objects has many Manager and QuerySet, it causes my models.py to be unreadable and so long. How do I manage them?

I come up with a solution. Originally. Model, Manager and QuerySet are in models.py. I want to separate from them and make independent files, for example, managers.py, queryset.py.

This is my original directory.

├── views.py
├── urls.py
├── models.py
└── ...

This is new directory by my method.

├── views.py
├── urls.py
├── models.py
├── managers.py
├── queryset.py
└── ...

Is is okay? or anyone has better propose?

Tony
  • 1,019
  • 2
  • 13
  • 25
  • 1
    Thats actually a nice approach when you have lots of code for custom managers. – Rajesh Kaushik Aug 12 '15 at 02:40
  • This is quite arbitrary, you should do whatever works best for _you_. Both your approach and François' approach are valid solutions. – knbk Aug 12 '15 at 12:06

1 Answers1

2

I personally prefer to split the models.py file if it's too long.

Delete models.py, create a director instead. Under the folder models

  • __init__.py - with the includes:
    • from .group1 import Model1, Model2
    • from .group2 import Model3, Model4
  • group1.py - contains Model1, Model1QuerySet, Model2, Model2QuerySet
  • group2.py - contains Model3, etc.

Obviously you want some nice meaningful names instead of group1 & group2.

François Constant
  • 5,531
  • 1
  • 33
  • 39