There are various things wrong with your Manager
:
- You're subclassing
Model
, notManager
. - You're using your model attributes as if they belonged to the
Manager
, which they don't. - Your custom
get_queryset
isn't calling the superclass method, so it's using an undefinedqs
attribute.
The correct way to define your manager would be:
class CurrentHospitalizationManager(models.Manager):
def get_query_set(self):
qs = super(CurrentHospitalizationManager, self).get_query_set()
today = date.today()
return qs.filter(
# we can use Q objects here to check if the date_out exists or not
# and compare against the current date if necesary
models.Q(date_out__isnull=True) | models.Q(date_out__gte=today),
date_in__lte=today
)
Then you should assign the manager to a class attribute on your model, like this
class Hospitalization(models.Model):
current_objects = CurrentHospitalizationManager()
...
And use it on your code like this:
Hospitalization.current_objects.get(...) # or filter, or whatever
I don't recommend you assign this custom manager to your default manager attr (objects
), since you wouldn't be able to access the Hospitalization
's instances that aren't "current".