使用自定义函数** Django 1.3重载admin search_field

时间:2013-12-20 03:12:56

标签: python django

我在一个项目中遇到了这个问题,我正试图在管理站点中搜索一系列不同的多边形,然后根据自定义的point_in_poly函数返回包含该点的多边形列表。

在models.py中我有一个坐标模型......

    class PointsXY(models.Model):
        lat = models.cs_value() #custom code
        lon = models.cs_value()

    class ImagesDB(models.Model):
        ptsxy = models.ForeignKey('PointsXY')
        # more model pieces added here

        def point_in_poly(self,x,y,polygon):
            # point in poly code

在我的admin.py文件中

    class PointsXYInline(Inline):
        model = coordinates

    class ImagesDBAdmin(admin.ModelAdmin):
        fieldsets[# bunch of fields placed
            ]
        # inline goes here
        search_fields['???'] # not sure where to go from here. 
        # can I override the search function to search the polygons and   
        # display only those that have said point in them?

如果您需要更多代码,我可以添加更多但我希望这已经足够了。

P.S。这是在iPad上输入的,所以我试图保持简短。

提前致谢!

1 个答案:

答案 0 :(得分:0)

当然你可以,doc说你可以使用方法get_search_fields

示例可以是:

class ImagesDBAdmin(admin.ModelAdmin):
        fieldsets[# bunch of fields placed
            ]
        def get_search_fields(self, request):
             search_fields = ['ptsxy']
             if "localhost:8000" in request.META['HTTP_REFERER']:
                  search_fields = ["ptsxy__lat"]
             return search_fields