1

我遇到了非常奇怪的 django 管理站点行为。
这是我的admin.py

from django.contrib import admin
from pastebin.models import Paste
from datetime import date

class ExpiredListFilter(admin.SimpleListFilter):

    title = u"Expiration"
    parameter_name = "expiration_date"

    def lookups(self, request, model_admin):
        return (
            ('yes', u'expired'),
            ('no', u'actual'),
        )

    def queryset(self, request, queryset):
        if self.value == 'yes':
            return queryset.filter(expiration_date__lt=date.today())
        if self.value == 'no':
            return queryset.filter(expiration_date__gte=date.today())


class AuthExpiredListFilter(ExpiredListFilter):

    def lookups(self, request, model_admin):
        if request.user.is_superuser:
            return super(AuthExpiredListFilter, self).lookups(request, model_admin)

    def queryset(self, request, queryset):
        if request.user.is_superuser:
            return super(AuthExpiredListFilter, self).queryset(request, queryset)


class PasteAdmin(admin.ModelAdmin):
    date_hierarchy = "created_at"
    fields = ('author', 'title', 'content', 'syntax', 'expiration_date')
    list_display = ('title', 'author', 'syntax', 'is_expired')
    list_display_links = ('title',)
    list_filter = (AuthExpiredListFilter,)


admin.site.register(Paste, PasteAdmin)

我只是想创建自己的过滤器。应用程序是关于创建粘贴...类似于 pastebin。所以每个粘贴都有它的有效期。但是管理站点没有显示过期的粘贴。objects.all()我可以通过调用或直接查找在 shell 中看到它objects.get(pk=)
这是我的model.py

from django.db import models
from django.contrib.auth.models import User
from datetime import date, datetime, timedelta


class ExpQuerySet(models.query.QuerySet):
    '''
    QuerySet for administrative purpose only. Gives access to expired Pastes 
    to archive and delete them
    '''

    def actual(self):
        return self.filter(expiration_date__gte=date.today())

    def expired(self):
        return self.filter(expiration_date__lt=date.today())


class ActualExpiredManager(models.Manager):
    '''
    Class to manage ExpQuerySet. Gives access to it's 
    actual() and expired() methods.
    '''

    def get_query_set(self):
        return ExpQuerySet(self.model)

    def __getattr__(self, attr, *args):
        if attr.startswith("_") or attr.startswith("__"):
            raise AttributeError
        return getattr(self.get_query_set(), attr, *args)


class ActualManager(models.Manager):
    '''
    Actual manager to work with. Returns a QuerySet of actual Pastes.
    '''

    def get_query_set(self):
        return ExpQuerySet(self.model).actual()

    def __getattr__(self, attr, *args):
        if attr.startswith("_") or attr.startswith("__"):
            raise AttributeError
        return getattr(self.get_query_set(), attr, *args)



class Paste(models.Model):

    SYNTAX_CHOICES = (
        ('txt', u'Text'),
        ('py', u'Python'),
        ('js', u'JavaScript'),
        ('rb', u'Ruby'),
        ('pl', u'Perl'),
        ('asp', u'ASP.NET'),
        ('java', u'Java'),
    )

    title = models.CharField(max_length=100, blank=True, verbose_name=u"Paste's Title")
    author = models.ForeignKey(User, null=True, verbose_name=u"Paste's Author")
    content = models.TextField()
    syntax = models.CharField(max_length=5, choices=SYNTAX_CHOICES, default='txt')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name=u"Was created at")
    modified_at = models.DateTimeField(auto_now=True, verbose_name=u"Was modified at")
    expiration_date = models.DateField(verbose_name=u"Date to expire")

    _objects = ActualManager() # it was objects.. I thought this is a cause of a problem so I've added underscore and simple Manager
    act_exp = ActualExpiredManager()
    objects = models.Manager()

    def __unicode__(self):
        return self.title

    def save(self, *args, **kwargs):
        if len(self.title) == 0:
            self.title = u"Untitled Post"
        super(Paste, self).save(*args, **kwargs)

    class Meta:
        ordering=['-created_at']

    def was_modified(self):
        r_range = self.created_at + timedelta(minutes=2)
        if self.modified_at < r_range:
            return False
        return True
    was_modified.short_description = "Was modified"
    was_modified.boolean = True

    def is_expired(self):
        if self.expiration_date < date.today():
            return True
        return False
    is_expired.short_description = "Is expired"
    is_expired.boolean = True

我在哪里做错了?
django错误截图它无法粘贴 id=6
控制台截图一切都好
提前谢谢。也许有人会指出我的错误。

4

0 回答 0