在我的django管理项目中,我有现场平衡。 我想在2000-5000,5000-7000 ...... value1-vvalue2,value3-value4..and用户想要的范围之间应用搜索余额,但我不知道如何在我的代码中应用它。我已按照名称,城市等应用了常规搜索...我的代码正在关注
models.py
cust_id=models.CharField(max_length=50)
name_regex = RegexValidator(regex=r'^[a-zA-Z\s]+$', message="only characters are allowed")
cust_first_name=models.CharField(validators=[name_regex],max_length=50)
cust_middle_name=models.CharField(validators=[name_regex],max_length=50,blank=True, null=True)
cust_last_name=models.CharField(validators=[name_regex],max_length=50,blank=True, null=True)
father_name=models.CharField(validators=[name_regex],max_length=50)
village_name=models.CharField(validators=[name_regex],max_length=50)
balance=models.PositiveIntegerField(default=0,blank=True, null=True)
Admin.py
from django.contrib import admin
from account.models import *
from import_export import resources
from django import forms
from import_export.admin import ImportExportModelAdmin
# from webcam.fields SSSSSSimport CameraField
from django.contrib.admin.options import FORMFIELD_FOR_DBFIELD_DEFAULTS
from webcam import widgets
from webcam.fields import CameraField
FORMFIELD_FOR_DBFIELD_DEFAULTS[CameraField] = {'widget': widgets.CameraWidget}
class AccounttResource(resources.ModelResource):
class Meta:
model=Account
import_id_fields = ['cust_id']
# fields=('cust_first_name','balance','village_name')
class AccountAdmin(ImportExportModelAdmin,admin.ModelAdmin):
# class AccountAdmin(admin.ModelAdmin):
# list_filter = [ValueRangeFilter, ]
search_fields=['cust_id','cust_first_name','village_name','balance' ]
resource_class = AccounttResource
list_display = ['cust_first_name','cust_id']
list_display_links = ('cust_first_name',)
readonly_fields=('image_tag_thumb','image_tag','cust_id','balance')
class ReportingResource(resources.ModelResource):
class Meta:
model=Account
import_id_fields = ['cust_id']
fields=('cust_first_name','balance','village_name')
class ReportingAdmin(ImportExportModelAdmin,admin.ModelAdmin):
# form=AccountPhone
# fields = ('cust_first_name', 'balance')
# list_filter = [ValueRangeFilter, ]
search_fields=['cust_id','cust_first_name','village_name','balance' ]
resource_class = ReportingResource
list_display = ['cust_first_name','balance','village_name','total_payment_received']
list_display_links = ('cust_first_name',)
readonly_fields=('image_tag_thumb','image_tag','cust_id',)
admin.site.register(Account,AccountAdmin)
admin.site.register(Reporting,ReportingAdmin)
现在该怎么做才能在我的代码中为平衡字段应用范围搜索。
答案 0 :(得分:0)
请查看docs以获取更多信息。
class ReportingAdmin(ImportExportModelAdmin,admin.ModelAdmin):
...
def get_search_results(self, request, queryset, search_term):
range = search_term.split('-')
number1 = None
number2 = None
try:
if len(range>2):
number1 = int(range[0])
number1 = int(range[1])
search_term = ''
except ValueError:
pass
queryset, use_distinct = super(ReportingAdmin, self).get_search_results(request, queryset, search_term)
if number1 and number2:
queryset |= self.model.objects.filter(balance__gte=number1, balance__lte=number2)
return queryset, use_distinct