28

I'm working on some Django-code that has a model like this:

class Status(models.Model):
    code = models.IntegerField()
    text = models.CharField(maxlength=255)

There are about 10 pre-defined code/text-pairs that are stored in the database. Scattered around the codebase I see code like this:

status = Status.objects.get(code=0) # successful
status = Status.objects.get(code=1) # failed

I would rather have a method for each so that the code would look something like this instead:

status = Status.successful()
status = Status.failed()
etc...

Is this possible? I have looked in to the Manager-stuff but I haven't really found a way. Is it time to really RTFM?

In Java it would be a static method and in Ruby you would just define a method on self, but it's not that easy in Python, is it?

1 Answer 1

51

You should perhaps implement this by defining a custom manager for your class, and adding two manager methods on that manager (which I believe is the preferred way for adding table-level functionality for any model). However, another way of doing it is by throwing in two class methods on your class that query and return resulting objects, such as:

class Status(models.Model):
    code = models.IntegerField()
    text = models.CharField(maxlength=255)

    @classmethod
    def successful(cls):
        return cls.objects.get(code=0)

    @classmethod
    def failed(cls):
        return cls.objects.get(code=1)

Do note please that get() is likely to throw different exceptions, such as Status.DoesNotExist and MultipleObjectsReturned.

And for an example implementation of how to do the same thing using Django managers, you could do something like this:

class StatusManager(models.Manager):
    def successful(self):
        return self.get(code=1)

    def failed(self):
        return self.get(code=0)

class Status(models.Model):
    code = models.IntegerField()
    text = models.CharField(maxlength=255)

    objects = StatusManager()

Where, you could do Status.objects.successful() and Status.objects.failed() to get what you desire.

4
  • Manager method is the way. Not only is very flexible, but also very expressive!
    – Agos
    Feb 6, 2010 at 14:26
  • Pardon me if it is a stupid question, but what is the harm in defining these methods in the model directly without creating any additional managers? That would just register the methods under the default manager. Unless you want to switch managers depending on circumstances, and thus implement different sets of methods, it should work, should it not? Aug 12, 2013 at 10:02
  • 2
    @SubhamoySengupta: There is no harm in doing it the way you describe, as far as I know. However, keeping such methods reserved for managers conforms to Django's conventions and helps keep things clean, because methods in managers work against a particular table, while methods in models work against a particular record or row. If your requirement is to operate over a table, then a manager is the Django way to go about doing it.
    – ayaz
    Aug 14, 2013 at 19:37
  • 1
    @SubhamoySengupta just it s architecture choice default manager by convention is objects... you can add as many as you want... if you are using django 1.7 is better to subclass QuerySet so you can chain queries and you can do objects = CustomQuerySet.as_manager()...
    – carlitux
    Jun 17, 2015 at 17:09

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.