2
There exists a method, django.db.models.get_model()
, that gets a model given its name. This’ll fix it as you aren’t actually importing the model.
0
Your get_future_shifts()
method implies that Shift
has a FK on Mentor
. In this case, you don’t need to import Shift
in the module containing Mentor
, you can just use the reverse relationship ie:
class Mentor(models.Model):
# ...
def get_future_shifts(self):
return self.shift_set.filter(session__date__gt=timezone.now())
but this will only “technically” solve the circular dependency, since it means that Mentor
has some direct knowledge of another app that itself depends on the app where Mentor
is defined and the whole thing will break if you remove this other app from your settings.
Another solution is to define get_future_shifts
in the same app as Shift' and monkeypatch
Mentor`, ie:
from otherapp.models import Mentor
class Shift(models.Model):
mentor = models.ForeignKey(Mentor)
# Extend Mentor with get_future_shifts helper
def get_future_shifts(mentor):
return mentor.shift_set.filter(session__date__gt=timezone.now())
Mentor.get_future_shifts = get_future_shifts
Some will frown upon extending a class from another module / app, but just defining a FK on Mentor here is already extending Mentor
, so at least we keep related stuff together and now Mentor
has no direct dependency on Shift
– as long as nothing in Mentor
‘s app’s views etc depends on get_future_shifts()
at least, but then it means that Shifts
should really belong to the same app as Mentor
.
- [Answered ]-Regex to match combination of letter and numbers to be used in URL pattarns in Django Project?
- [Answered ]-Django, how to make a query with [ORDER BY CONVERT (name USING gbk) ASC]
- [Answered ]-Django Braintree : KeyError at /payments-billing/
- [Answered ]-Fixing request entity too large error under django/apache/mod_wsgi for file uploads
- [Answered ]-Empty Chatterbot Conversation table in Django Admin