jrief/django-admin-sortable2

Gives an error when in the sorted Inlines formset I override the clean method

mrfoggg opened this issue · 3 comments

BaseFormSet.init() got an unexpected keyword argument 'default_order_direction'

class GroupPlacementInlineFS(forms.models.BaseInlineFormSet):
    def clean(self):
        if check_change_field_in_fs(self, "group"):
            groups = []
            for form in self.forms:
                if self.can_delete and self._should_delete_form(form):
                    continue
                if (group := form.cleaned_data["group"]) in groups:
                    raise ValidationError(f'Группа {group} дублируется')
                groups.append(group)
                for product in Product.objects.filter(categories=self.instance):
                    for category in product.categories.all():
                        if category == self.instance:
                            continue
                        if category.groups.contains(group):
                            raise ValidationError(f'Для товара "{product}" который содержится так же и в категории '
                                                  f'"{category}" группа атрибутов "{group}" уже определена')
`class GroupPlacementInline(SortableTabularInline, admin.TabularInline):
    formset = GroupPlacementInlineFS
    readonly_fields = ('attributes_list',)
    model = GroupPlacement
    extra = 0
    fields = ('group', 'attributes_list', 'position')
    ordering = ['position']
    @admin.display(description='Содержжит атрибуты')
    def attributes_list(self, obj):
        return obj.group.attributes_list`

@admin.register(Category)
class CategoryAdmin(DraggableMPTTAdmin, SummernoteModelAdmin, SortableAdminBase, admin.ModelAdmin, ):
    form = CategoryForm
    save_on_top = True
    list_display = ('tree_actions', 'indented_title', 'groups_list',)
    prepopulated_fields = {"slug": ("name",)}
    summernote_fields = ('description',)
    save_as = True
    save_as_continue = False
    inlines = (GroupPlacementInline, MainAttributeInLine, ShotAttributeInLine, ProductPlacementInlineForCategory,
               FilterInline,)
    actions = [export_as_json]

    class Media:
        ordering = ['name']
        default_order_field = 'name'
        default_order_direction = ''

        css = {
            "all": ("category_admin.css",)
        }
jrief commented

Please read how to report bugs.

class ChapterTabularInlineFS(forms.models.BaseInlineFormSet):
pass

class ChapterTabularInline(SortableTabularInline):
formset = ChapterTabularInlineFS
model = Chapter
extra = 1
ordering = ['my_order']

Environment:

Request Method: GET
Request URL: http://localhost:8000/admin/testapp/book5/1/change/

Django Version: 4.1.2
Python Version: 3.11.0
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.admin',
'django.contrib.staticfiles',
'django.contrib.messages',
'adminsortable2',
'testapp']
Installed Middleware:
['django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'testapp.middleware.AutoLoginMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.csrf.CsrfViewMiddleware']

Traceback (most recent call last):
File "/home/snip_manager/Документы/projects/django-admin-sortable2/sotable2/lib64/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/home/snip_manager/Документы/projects/django-admin-sortable2/sotable2/lib64/python3.11/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/snip_manager/Документы/projects/django-admin-sortable2/sotable2/lib64/python3.11/site-packages/django/contrib/admin/options.py", line 686, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/snip_manager/Документы/projects/django-admin-sortable2/sotable2/lib64/python3.11/site-packages/django/utils/decorators.py", line 133, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/snip_manager/Документы/projects/django-admin-sortable2/sotable2/lib64/python3.11/site-packages/django/views/decorators/cache.py", line 62, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/home/snip_manager/Документы/projects/django-admin-sortable2/sotable2/lib64/python3.11/site-packages/django/contrib/admin/sites.py", line 242, in inner
return view(request, *args, **kwargs)
File "/home/snip_manager/Документы/projects/django-admin-sortable2/sotable2/lib64/python3.11/site-packages/django/contrib/admin/options.py", line 1893, in change_view
return self.changeform_view(request, object_id, form_url, extra_context)
File "/home/snip_manager/Документы/projects/django-admin-sortable2/sotable2/lib64/python3.11/site-packages/django/utils/decorators.py", line 46, in _wrapper
return bound_method(*args, **kwargs)
File "/home/snip_manager/Документы/projects/django-admin-sortable2/sotable2/lib64/python3.11/site-packages/django/utils/decorators.py", line 133, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/snip_manager/Документы/projects/django-admin-sortable2/sotable2/lib64/python3.11/site-packages/django/contrib/admin/options.py", line 1750, in changeform_view
return self._changeform_view(request, object_id, form_url, extra_context)
File "/home/snip_manager/Документы/projects/django-admin-sortable2/sotable2/lib64/python3.11/site-packages/django/contrib/admin/options.py", line 1824, in _changeform_view
formsets, inline_instances = self._create_formsets(
File "/home/snip_manager/Документы/projects/django-admin-sortable2/sotable2/lib64/python3.11/site-packages/django/contrib/admin/options.py", line 2268, in _create_formsets
formset = FormSet(**formset_params)
File "/home/snip_manager/Документы/projects/django-admin-sortable2/sotable2/lib64/python3.11/site-packages/django/forms/models.py", line 1102, in init
super().init(data, files, prefix=prefix, queryset=qs, **kwargs)
File "/home/snip_manager/Документы/projects/django-admin-sortable2/sotable2/lib64/python3.11/site-packages/django/forms/models.py", line 678, in init
super().init(

Exception Type: TypeError at /admin/testapp/book5/1/change/
Exception Value: BaseFormSet.init() got an unexpected keyword argument 'default_order_direction'

Found a solution on Stackoverflow

`class ChapterTabularInlineFS(CustomInlineFormSet, forms.models.BaseInlineFormSet):
    pass

class ChapterTabularInline(SortableTabularInline):
    formset = ChapterTabularInlineFS
    model = Chapter
    extra = 1
    ordering = ['my_order']`