I trying to filter the list shown when I use the lupe in many to many fields (image bellow). Maybe a text search would be interesting too.
Any help?
Here's the code:
class PresentialModuleCourseInline(NestedStackedInline):
"""Module Course Presential Stacked Inline"""
model = Course.modules.through
raw_id_fields = ('module_course',)
extra = 1
def get_queryset(self, request):
return self.model.objects.filter(
module_course__type_course=ModuleCourse.PRESENTIAL) # Doesn't work
self.model
is not QuerySet
or a model, use the model directly:
class PresentialModuleCourseInline(NestedStackedInline):
model = Course.modules.through
raw_id_fields = ('module_course',)
extra = 1
def get_queryset(self, request):
return Course.objects.filter(module_course__type_course=ModuleCourse.PRESENTIAL)
For searching set search_fields
for your CourseAdmin
:
class CourseAdmin(admin.ModelAdmin):
search_fields = ('title',) # your search fields here
To make filter in ForeignKeyRawIdWidget
(lupe) you need to add a limit_choices_to the widget, it add a query param to filter in popup page like ?type_course=online
Sample:
class PresentialModuleCourseInline(NestedStackedInline):
"""Module Course Presential Stacked Inline"""
model = Course.modules.through
extra = 1
raw_id_fields = ('module_course', )
def get_formset(self, request, obj=None, **kwargs):
form = super().get_formset(request, obj, **kwargs)
field = form.form.base_fields['module_course']
field.widget.rel.limit_choices_to =\
{'type_course': ModuleCourse.PRESENTIAL}
return form
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.