No expert on Django, and it's hard to tell what your GOAL is from this
description, but you might want I'd write something like this:
class Employee(models.Model):
name = models.CharField(...)
class Manager(Employee):
manager_stuff...
class Worker(Employee):
worker_stuff...
manager = models.ForeignKey(Manager,related_name="subordinates",
blank=True, null=True)
class WorkerTypeA(Worker):
workera_stuff..
class WorkerTypeB(Worker):
workerb_stuff...
class ManagerB(Manager):
a_person = ForeignKey(
more_manager_b_stuff...
Again, depending on what your goal is, you might not even need that
a_person field now, since Workers can have managers, and managers can
have subordinates.
--
Lee
Since you don't tell us what you're doing currently, we'll have to break
out the crystal ball and see if the vapours let us guess as to the false
starts you might have made. It's hard to correct existing code without
knowing what it is. :-)
However, there shouldn't be any circular import problems. You just have
to remember to import only the module, not names inside the module in
one of the files. To wit...
In models.py:
from django.db import models
from managers import BManager
class A(models.Model):
...
class B(models.Model):
...
objects = BManager()
In managers.py:
from django.db import models
import models
class BManager(models.Manager):
...
# Refer to models.ModelA inside methods without problems.
When Python is importing models.py, it will see that it has to import
managers.py. So it does that, which says "import models". Python knows
that it has already imported (or is in the process of importing)
models.py, so it doesn't do it again, it just continues on. Providing it
does not need to refer to the contents of "models" (e.g. models.ModelA)
whilst parsing the managers module, there is no problem.
Circular import problems arise if you try to do "from models import
ModelA" in managers, because models isn't yet fully imported, so Python
cannot look up the ModelA inside that module. Problems would also arise
if you wrote something like this:
class BManager(models.Manager):
fred = models.ModelA.objects.all()
since the class definition is processed (executed) at parsing time,
which would require access to models.ModelA, which isn't available yet.
But that wouldn't be particularly sensible in any case (ModelA's
contents are dynamic, so reading it only at import time is inflexible).
If you have something like this:
class BManager(models.Manager):
def fred(self):
return models.ModelA.objects.all()
Then the statements inside fred() are not executed until the method is
called (they are parsed, but not executed, when the file is imported).
When you get around to calling method fred(), "models" will be fully
imported and there will be no problems.
>
> Assuming that I need to separate models and manager into different
> files, so merging them is not an option,
If you have some particularly twisted set up, then you'll simply need to
drop that assumption. It's not really a requirement for functionality,
merely a style issue. And sometimes style has to take a backseat to
practicality.
> are there any best practices
Best practice is "whatever works and is maintainable". People get far
too hung up on sticking a best practice label on things to the point
that it becomes devalued. Sure, some approaches look neater than others,
but I see too many people spinning their wheels wondering about the
unique "best" (in some truly debatable sense) solution, rather than just
getting on and solving their problems.
Regards,
Malcolm