class Dude(models.Model): abides = models.BooleanField(default=True) name = models.CharField(max_length=20) has_rug = models.BooleanField(default=False) objects = PassThroughManager(DudeQuerySet) abiders = AbidingManager()
class Person(models.Model): first_name = models.CharField(max_length=100) last_name = models.CharField(max_length=100) birthday = models.DateField() objects = PassThroughManager(PersonQuerySet) def __str__(self): return '%s %s, Birthday: %s' % (self.first_name, self.last_name, self.birthday) class Meta: ordering = ('last_name', )
class AbstractLive(models.Model): is_enabled = models.BooleanField('enabled', db_index=True, default=True) is_featured = models.BooleanField('featured', db_index=True, default=False) time_publish = models.DateTimeField(db_index=True, default=timezone.now) """:py:class:`datetime` for when this object was published.""" user_publish = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True) """User who last published this object (required).""" time_expire = models.DateTimeField(db_index=True, null=True, blank=True) """:py:class:`datetime` for when this object expires.""" objects = PassThroughManager().for_queryset_class(LiveQuerySet)() class Meta(object): abstract = True get_latest_by = 'time_publish' def is_active(self): return self in self.__class__.objects.filter(pk=self.pk).active()
class Car(models.Model): name = models.CharField(max_length=20) owner = models.ForeignKey(Dude, related_name='cars_owned') objects = PassThroughManager(DudeQuerySet)
class PostBase(TimeStampedModel): """ Base class for Post-like models This class defines basic fields and logic for a blog post, including author, title, status (whether it's published or not) and content, plus timestamp fields. By default, it uses an InheritanceManager, allowing subclasses to be included in a queryset. """ STATUS = Choices( ('draft', 'Draft'), ('public', 'Public'), ('hidden', 'Hidden'), ) # metadata author = models.ForeignKey(User) published = models.DateTimeField(blank=True, null=True) status = StatusField(default=STATUS.draft) status_changed = MonitorField(monitor='status', editable=False) # content title = models.CharField(max_length=255, blank=True) slug = models.SlugField(max_length=255) excerpt = models.TextField(blank=True, help_text="Add a manual excerpt") content = SplitField(blank=True) allow_comments = models.BooleanField(default=True) tags = TaggableManager(blank=True) # manager objects = PassThroughManager(PostQuerySet) versions = LatestManager() class Meta: abstract = True get_latest_by = "published" ordering = ('-published', '-created') def __unicode__(self): return self.title @models.permalink def get_absolute_url(self): return ('scrivo_post_detail', None, { 'year': self.published.strftime('%Y'), 'month': self.published.strftime('%b').lower(), 'day': self.published.strftime('%d'), 'slug': self.slug }) def publish(self, *args, **kwargs): if not self.published: self.published = datetime.datetime.now() self.status = self.STATUS.public self.save(*args, **kwargs) def save(self, *args, **kwargs): if not self.slug: self.slug = slugify(self.title) super(PostBase, self).save(*args, **kwargs)