我正在尝试设置Django过滤器,但仍然看到如下所示的错误消息
EmployeeFilterSet解析的字段“ emp_photo”具有“精确”查找到无法识别的字段类型ImageField。尝试将替代添加到“ Meta.filter_overrides”。参见:https://django-filter.readthedocs.io/en/master/ref/filterset.html#customise-filter-generation-with-filter-overrides
据我所知,我遵循了文档as seen here。
我的模特:
from __future__ import unicode_literals
import django_filters
from django.core.validators import RegexValidator
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
@python_2_unicode_compatible
class Employee(models.Model):
# basic information of employee
first_name = models.CharField(_('first name'), max_length=40)
last_name = models.CharField(_('last name'), max_length=40)
emp_photo = models.ImageField(_('passport'))
date_of_birth = models.DateField(_('birthday'))
gender = models.CharField(_('gender'), max_length=15, choices=GENDER_CHOICES, default=['MALE', 'Male'])
house_address = models.CharField(_('house address'), max_length=50)
city_of_residence = models.CharField(_('city'), max_length=40)
state_of_residence = models.CharField(_('state'), max_length=40, choices=NIGERIAN_STATE_CHOICES)
country_of_residence = models.CharField(_('country'), max_length=40, choices=COUNTRY_CHOICES,
default=[156, 'Nigeria'])
state_of_origin = models.CharField(_('state of origin'), max_length=40, choices=NIGERIAN_STATE_CHOICES)
local_govt_of_origin = models.CharField(_('LGA of origin'), max_length=40)
town_or_city_of_origin = models.CharField(_('town or city'), max_length=40)
nationality = models.CharField(_('nationality'), max_length=40, choices=COUNTRY_CHOICES, default=[156, 'Nigeria'])
email = models.EmailField(_("email address"))
phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$',
message="phone number must be entered in the "
"format: '+2340000000000'. Up to 15 digits allowed.")
phone_number = models.CharField(_('phone number'), validators=[phone_regex], max_length=17, blank=True)
# employment record of employee
emp_id = models.IntegerField(unique=True, primary_key=True, default=1)
job_title = models.CharField(_('job title'), max_length=50)
dept_name = models.CharField(_('department name'), max_length=50)
branch_name = models.CharField(_('branch name'), max_length=50)
hire_date = models.DateField(_('hire date'))
class Meta:
verbose_name = _('Employee')
verbose_name_plural = _('Employees')
def __str__(self):
return "{} {}".format(self.first_name, self.last_name)
class EmployeeFilter(django_filters.FilterSet):
first_name = django_filters.CharFilter(method='custom_filter')
class Meta:
model = Employee
exclude = ['emp_photo']
fields = ['first_name', 'last_name', 'branch_name']
def custom_filter(self, queryset, value, first_name=None):
return queryset.filter(**{
first_name: value,
})
表格:
import django_tables2 as tables
from .models import Employee
class EmployeeTable(tables.Table):
class Meta:
model = Employee
template_name = "django_tables2/bootstrap-responsive.html"
fields = ("emp_id", "first_name", "last_name", "job_title", "dept_name", "hire_date")
export_formats = ['csv', 'xlsx']
观看次数:
from django.contrib.auth.decorators import login_required
from django.shortcuts import render
from django_tables2 import RequestConfig
from django_tables2.export import TableExport
from .models import Employee
from .models import EmployeeFilter
from .tables import EmployeeTable
@login_required()
def employees(request):
filter = EmployeeFilter(request.GET, queryset=Employee.objects.all())
table = EmployeeTable(filter.qs)
RequestConfig(request).configure(table)
export_format = request.GET.get("_export", None)
if TableExport.is_valid_format(export_format):
exporter = TableExport(export_format, table)
return exporter.response("table.{}".format("csv", "xlsx"))
return render(request, "employees/employees.html", {
"table": table,
"filter": filter
})
模板:
<!--Employees data-->
<div class="filter-container container-fluid">
<div class="row">
<div class="col-md-10">
<!--filter form-->
<form action="" class="form form-inline employee-search-form" method="get">
{% bootstrap_form filter.form layout='inline' %}
{% bootstrap_button 'filter' %}
</form>
</div>
<div class="col-md-2">
<div class="btn-group download-btn">
<button aria-expanded="false" aria-haspopup="true"
class="btn btn-warning dropdown-toggle"
data-toggle="dropdown" type="button">
Download
</button>
<div class="dropdown-menu">
<a class="dropdown-item" href="{% querystring '_export'='csv' %}">csv</a>
<a class="dropdown-item" href="{% querystring '_export'='xlsx' %}">xlsx</a>
</div>
</div>
</div>
</div>
</div>
<!-- data rendered here -->
{% render_table table 'django_tables2/bootstrap.html' %}
</div>
如果有人可以帮助我,我将非常感激。
答案 0 :(得分:1)
django过滤器不支持ImageField
。您只需要向过滤器集中添加具有受支持类型的字段即可。
views.py
from django.contrib.auth.decorators import login_required
from django.shortcuts import render
from django_tables2 import RequestConfig
from django_tables2.export import TableExport
from .models import Employee
from .filters import EmployeeFilter
from .tables import EmployeeTable
@login_required()
def employees(request):
filter = EmployeeFilter(request.GET, queryset=Employee.objects.all())
table = EmployeeTable(filter.qs) # You needed to associate filter to table to make it work.
RequestConfig(request).configure(table)
export_format = request.GET.get("_export", None)
if TableExport.is_valid_format(export_format):
exporter = TableExport(export_format, table)
return exporter.response("table.{}".format("csv", "xlsx"))
return render(request, "employees/employees.html", {
"table": table,
"filter": filter
})
employees.html
{% load render_table from django_tables2 %}
{# Load the tag library #}
{% load bootstrap3 %}
{# Load CSS and JavaScript #}
{% bootstrap_css %}
{% bootstrap_javascript %}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Books</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>
<!--Employees data-->
<div class="filter-container container-fluid">
<div class="row">
<div class="col-md-10">
<!--filter form-->
<form action="" class="form form-inline employee-search-form" method="get">
{% bootstrap_form filter.form layout='inline' %}
{% bootstrap_button 'filter' %}
</form>
</div>
<div class="col-md-2">
<div class="btn-group download-btn">
<button aria-expanded="false" aria-haspopup="true"
class="btn btn-warning dropdown-toggle"
data-toggle="dropdown" type="button">
Download
</button>
<div class="dropdown-menu">
</div>
</div>
</div>
</div>
</div>
<!-- data rendered here -->
{% render_table table 'django_tables2/bootstrap.html' %}
</div>
</body>
</html>
EmployeeFilter
#I've changed this class to make it simple.
class EmployeeFilter(django_filters.FilterSet):
first_name = django_filters.CharFilter(lookup_expr='icontains')
class Meta:
model = Employee
fields = ["emp_id", "first_name", "last_name", "job_title", "dept_name", "hire_date"]
P.S。模型中未声明FilterSet上的某些字段。我想您只是忘记了对其进行编辑。