Created
May 2, 2017 20:20
-
-
Save augustogoulart/72f48710393d7b7040a53f76a64dd0e6 to your computer and use it in GitHub Desktop.
Simple model showing a custom manager usage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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