1👍
I don’t understand the way you write your manager. If the NATURAL_KEY
is an attribute of each model, I would write:
class NexchangeManager(models.Manager):
def get_by_natural_key(self, param):
qs = self.get_queryset()
if param == "*":
return qs.all()
lookup = {qs.model.NATURAL_KEY: param}
return qs.filter(**lookup)
0👍
Note: this answer is based on albars answer, but improved for allowing pk params.
manager:
class NexchangeManager(models.Manager):
def get_by_natural_key(self, param):
qs = self.get_queryset()
if param == "*":
return self.all()
lookup = {qs.model.NATURAL_KEY: param}
return self.get(**lookup)
Generic Model
class:
class NexchangeModel(models.Model):
class Meta:
abstract = True
objects = NexchangeManager()
Source:stackexchange.com