Hi,
I am writing a couple of django apps wich by design is coupled together. But i get sircular import problems. I know it might be bad design, so please give examples of better solutions, but i cant seem to find a better suited design. So if there isnt a better design, how to solve this one?
It is basically two django apps, with some models, wich relate to each other cross app-wise. In short the system is a event based system. So there is an event model and a task model. These live in different apps, Events and Tasks. When events is triggered, i need to check that surtain tasks are solved or not, and when a task is solved, that can trigger some other events.
So in events i need to store data about tasks (to check if these tasks are solved) and in tasks i need to store data about events (wich events to trigger when they are solved)
Here is some sample code from my apps:
Events app
models.py
from tasks.models import Task
class Event(models.Model):
...
tasks = models.ManyToManyField(Task, help_text=_("Tasks we need to check if are solved before triggering this event."))
...
Tasks app
models.py
from events.models import Event
class Task(models.Model):
...
events = models.ManyToManyField(Event, help_text=_("Events to trigger when this task i solved."))
...
This is causing import problems when i try to validate:
AttributeError: 'module' object has no attribute 'Event'
So how to solve this? Ive tried to use some of the django helper functions in hope that that would help, more specifically i've tried to use the django.db.models.get_app and get_model functions to import the models instead of directly importing them, but i still get problems.
Of course i could collect them into the same app, but i clearly belive they should live in separate apps, since they handle seperate things. But yes they are dependent on each other. If i cant solve the import problems, any ideas on how to design this different?
I could of course use some generic relations, but that would actually make things harder to understand for other people since it doesnt specify the content type that it should relate to.