我想知道,如何只显示“code_key”长度为 10 个字符的记录?
目前,如果我使用 'qs = Code.objects.filter(code_key.len ( )=10)' 返回错误。
提前致谢。
管理员.py
class CodeAdmin(admin.ModelAdmin):
fields = ['code_key','redemption_date','user','movie']
#
list_display = ('code_key','redemption_date','user','movie')
#
search_fields = ('code_key','user__email','movie__title')
def queryset(self, request):
"""
Filter the objects displayed in the change_list
"""
qs = super(CodeAdmin, self).queryset(request)
return qs
def changelist_view(self, request, extra_context=None):
my_context = {
'total': 'some value query',
}
return super(CodeAdmin, self).changelist_view(request,extra_context=my_context)
模型.py
class Code(models.Model):
id = models.AutoField(primary_key=True)
code_key = models.CharField(max_length=20,unique=True)
redemption_date = models.DateTimeField(null=True, blank=True)
user = models.ForeignKey(User, blank=True, null=True)
movie = models.ForeignKey(Movie, blank=True, null=True)
# ...
def display_record(self):
return (self.code_key.__len__() == 10)
# ...
def __unicode__(self):
return self.code_key