[英]Overload the admin search_field with a custom function ** Django 1.3
我在嘗試獲取一個點,在管理站點中搜索一系列不同的多邊形並基於自定義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?
如果您需要更多代碼片段,可以添加更多代碼,但我希望這足夠了。
附言:這是在iPad上輸入的,因此我試圖保持簡短。
提前致謝!
當然可以,正如文檔所述 ,您可以使用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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.