I have a manager in which I have
class MyManager(models.Manager):
TYPE_PENDING = 1
def pending():
return self.filter(type=self.TYPE_PENDING)
Now I can always get a queryset of pending objects.
But can I also do something to check if an object is pending?
I can of course use if object.type == MyManager.TYPE_PENDING: but can I do something in the manager, so I don't have to call MyManager.TYPE_PENDING outside the manager but instead create a function like
def is_pending():
return self.type == self.TYPE_PENDING
or something like that.
I could also do something like if object in Model.objects.pending(): but doesn't it have bad performance since it loads the entire queryset?
If I have two managers PackageManager and StateManager related to models Package and State, resp.
class PackageManager(models.Manager):
def delivered(self):
return self.filter(current_state__type=1)
class StateManager(models.Manager):
def delivered(self):
return self.filter(type=self.model.TYPE_DELIVERED)
I have my choices (TYPE_DELIVERED etc.) defined in State. Can I in PackageManager replace 1 with something telling that it should be TYPE_DELIVERED?
I know I can write current_state__type=State.TYPE_DELIVERED but it causes a circular import if I should import managers into models and models into managers.
Can I utilize that I can get a State queryset filtering for all delivered?
I mean if I can do something like
class PackageManager(models.Manager):
def delivered(self):
return self.filter(current_state__type=StateManager.model.TYPE_DELIVERED)
I think it would be more appropriate to define TYPE_PENDING in the model itself:
class MyModel(models.Model):
TYPE_PENDING = 1
...
@property
def is_pending(self):
return self.type == self.TYPE_PENDING
objects = MyManager()
...
class MyManager(models.Manager):
def pending():
return self.filter(type=self.model.TYPE_PENDING)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With