Anthony Nandaa
Anthony Nandaa

Reputation: 3400

Is this an okay way of doing Django Models

I am doing the fat models approach, so I have transformed my models.py into a package thus:

+--polls/
|  +--models/
|    +--__init__.py
|    +--__shared_imports.py
|    +--Choice.py
|    +--Question.py

My main part of the question is the __shared_imports.py: I realized that we've common import statements in various modules in the package and decided to to have that file to do the imports, then in my modules I write this:

from __shared_imports.py import *

Everything works fine, but just want to know if this approach is good. I'll appreciate your thoughts on this.

Upvotes: 0

Views: 116

Answers (2)

cleder
cleder

Reputation: 1797

avoid import * because it will prevent tools like pyflakes from determining undefined variables.

to move all of it in a subdirectory and splitting it into separate files is not a bad idea, albeit mostly not needed. when your models.py file gets big, you should rather be thinking about splitting the project up into smaller apps.

Upvotes: 1

Volodymyr Burenin
Volodymyr Burenin

Reputation: 137

  1. Use only lower case when you name modules.
  2. Do not use double underscore for module name.
  3. Read PEP8 and Google Python style guide.
  4. Use less verbose names for modules. For example: shared_imports.py -> shared.py

Got it.

In this case you need to import everything in __init__.py. Then you can export all names as __all__ = ['Choice', 'Question']

So, it will be enough just to import models package.

Example: __ init __.py

import Choice
import Question

__all__ = ['Choice', 'Question']

Upvotes: 2

Related Questions