简体   繁体   中英

Overload the admin search_field with a custom function ** Django 1.3

I'm having this issue on a project where I'm trying to take a point, search a series of different polygons in the admin site, and return a list of polygons that contain that point based on a custom point_in_poly function.

In models.py I have a coordinates model...

    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

In my admin.py file

    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?

If you need more snip-it's of code I can add more but I hope this is sufficient.

PS this was typed out on an iPad so I was trying to keep it brief.

Thanks in advance!

Of course you can, as the doc says you can use the method get_search_fields

an Example could be:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM