1

在对 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。

4

1 回答 1

0

在这里无法重现。使用模型.py:

class City( models.Model ):
    name = models.CharField(max_length=110)

class PiafInfo( models.Model ):
    no = models.IntegerField( u'No du Piaf', unique=True )
    origin_city = models.ForeignKey( City )

class PiafTrans( models.Model ):
    ttype         = models.IntegerField()
    date          = models.DateTimeField()
    piafSerial    = models.ForeignKey( PiafInfo )

管理员.py:

class PiafInfoAdmin( admin.ModelAdmin ):
    list_display = ('no', 'origin_city',)
    list_filter = ['origin_city', ]
    ordering = ('no',)
    search_fields = ('no',) 
admin.site.register( PiafInfo, PiafInfoAdmin )

class PiafTransAdmin( admin.ModelAdmin):
    list_display = ('date', 'piafSerial', 'ttype', )
    date_hierarchy = 'date'
    list_filter = ('ttype')
    search_fields = ('piafSerial', )
admin.site.register( PiafTrans, PiafTransAdmin )

django 1.3.8 pre-alfa(来自 1.3 分支的最新版本)

和以下代码:

startDate = datetime.today()
endDate = datetime.today()
piafInfos = PiafInfo.objects.all().distinct()
piafInfos = piafInfos.filter( piaftrans__date__range=(startDate,endDate) ).distinct()
于 2013-05-23T20:56:20.463 回答