在对 AdminModel 进行一些更改后,我的一个查询突然开始失败。
经过一番搜索,我发现在我的 admin_model 中添加一个 list_filter 正在创建 FieldError,这对我来说似乎真的很奇怪......
我的模型:
class PiafInfo( models.Model ):
no = models.IntegerField( u'No du Piaf', unique=True )
origin_city = models.ForeignKey( City )
...
class PiafInfoAdmin( admin.ModelAdmin ):
list_display = ('no', 'origin_city', 'group', 'fleet')
list_filter = ['origin_city', ]
ordering = ('no',)
search_fields = ('no', 'group', 'fleet')
admin.site.register( PiafInfo, PiafInfoAdmin )
class PiafTrans( models.Model ):
ttype = models.IntegerField( choices=TTYPE_CHOICES, default=TTYPE_RELOAD)
date = models.DateTimeField()
piafSerial = models.ForeignKey( PiafInfo )
...
class PiafTransAdmin( admin.ModelAdmin):
list_display = ('date', 'piafSerial', 'city', 'ttype', 'amount', 'subscrName' )
date_hierarchy = 'date'
list_filter = ('city', 'ttype')
search_fields = ('piafSerial', 'group', 'fleet', 'subscrName' )
admin.site.register( PiafTrans, PiafTransAdmin )
查询构成问题(第二行):
piafInfos = models.PiafInfo.objects.all().distinct()
piafInfos = piafInfos.filter( piaftrans__date__range=(startDate,endDate) ).distinct()
像这样,我收到以下错误:
FieldError: Cannot resolve keyword 'piaftrans' into field. Choices are: fleet, group, id, no, origin_city
它试图将 piaftrans 解释为字段名称而不是模型。
如果我在 PiafInfoAdmin 中评论一行:
class PiafInfoAdmin( admin.ModelAdmin ):
list_display = ('no', 'origin_city', 'group', 'fleet')
# list_filter = ['origin_city', ]
ordering = ('no',)
search_fields = ('no', 'group', 'fleet')
admin.site.register( PiafInfo, PiafInfoAdmin )
错误消失。我看不出错误与我所做的更改之间有任何关联!
这适用于 Windows 上的 django 1.3 和 Python 2.7。