Skip to content

Instantly share code, notes, and snippets.

@augustogoulart
Created May 2, 2017 20:20
Show Gist options
  • Select an option

  • Save augustogoulart/72f48710393d7b7040a53f76a64dd0e6 to your computer and use it in GitHub Desktop.

Select an option

Save augustogoulart/72f48710393d7b7040a53f76a64dd0e6 to your computer and use it in GitHub Desktop.
Simple model showing a custom manager usage
class PublishedManager(models.Manager):
def get_queryset(self):
return super(PublishedManager, self).get_queryset().filter(status='published')
class Post(models.Model):
STATUS_CHOICES = (
('draft', 'Draft'),
('published', 'Published')
)
title = models.CharField(max_length=250)
slug = models.SlugField(max_length=250, unique_for_date='publish')
author = models.ForeignKey(User, related_name='blog_posts')
body = models.TextField()
publish = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='draft')
objects = models.Manager()
published = PublishedManager()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment