AutoCompleteFilter自我获取外键滤波器查询所有父母
我不想按父类别对类别进行过滤,我使用一个模型,对于父类别,我将其链接到自身。
模型:
class Category(models.Model):
name = models.CharField(max_length=100)
parent = models.ForeignKey(
"self", on_delete=models.CASCADE, null=True, blank=True, related_name="childs"
)
description = models.TextField(null=True, blank=True)
is_adult = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
在admin.py中:
class ParentCategoryFilter(AutocompleteFilter):
title = "By parent category"
field_name = "parent"
autocomplete_url = "parent-category-autocomplete"
is_placeholder_title = True
class CategoryAdmin(admin.ModelAdmin):
search_fields = ["name"]
autocomplete_fields = ["parent"]
fieldsets = (
(
_("Details"),
{
"fields": (
"name",
"parent",
"description",
),
},
),
)
list_display = (
"name",
"parent",
)
list_filter = [ParentCategoryFilter]
admin.site.register(Category, CategoryAdmin)
和views.py,其中定义了ParentCategoryFilter
:
class ParentCategoryAutocompleteView(autocomplete.Select2QuerySetView):
permissions = [
"CategoryView",
]
def get_queryset(self):
qs = Category.objects.filter(parent__isnull=True)
if self.q:
qs = qs.filter(Q(name__istartswith=self.q))
return qs
视图的url是:
urlpatterns = [
url(
"^/autocomplete",
staff_permission_required(ParentCategoryAutocompleteView.permissions)(
ParentCategoryAutocompleteView.as_view()
),
name="parent-category-autocomplete",
),
I wan't to make a filter on categories by parent category, I use a model and for parent category I linked it to itself.
the model:
class Category(models.Model):
name = models.CharField(max_length=100)
parent = models.ForeignKey(
"self", on_delete=models.CASCADE, null=True, blank=True, related_name="childs"
)
description = models.TextField(null=True, blank=True)
is_adult = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
in admin.py :
class ParentCategoryFilter(AutocompleteFilter):
title = "By parent category"
field_name = "parent"
autocomplete_url = "parent-category-autocomplete"
is_placeholder_title = True
class CategoryAdmin(admin.ModelAdmin):
search_fields = ["name"]
autocomplete_fields = ["parent"]
fieldsets = (
(
_("Details"),
{
"fields": (
"name",
"parent",
"description",
),
},
),
)
list_display = (
"name",
"parent",
)
list_filter = [ParentCategoryFilter]
admin.site.register(Category, CategoryAdmin)
and the views.py where ParentCategoryFilter
is defined:
class ParentCategoryAutocompleteView(autocomplete.Select2QuerySetView):
permissions = [
"CategoryView",
]
def get_queryset(self):
qs = Category.objects.filter(parent__isnull=True)
if self.q:
qs = qs.filter(Q(name__istartswith=self.q))
return qs
The url to the views is:
urlpatterns = [
url(
"^/autocomplete",
staff_permission_required(ParentCategoryAutocompleteView.permissions)(
ParentCategoryAutocompleteView.as_view()
),
name="parent-category-autocomplete",
),
the problem I get is that in the filter I get the child's categories name. I don't know what the problem with it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您没有在其他地方使用、调用或表示类别模型对象默认名称。一项简单的修改是向模型添加默认字符串名称。
这是一个快速而肮脏的解决方案。如果您无法使用此解决方案,我将进一步研究此问题。
If your not using, calling on , or representing the category model object default name anywhere else. One simple modification would be to add a default string name to the model.
This is a quick and dirty solution. If you are not able to use this solution I will look into this further.