openlibhums / books

A plugin for Janeway that displays books.
0 stars 0 forks source link

Crash while adding chapters #47

Open gamboz opened 10 months ago

gamboz commented 10 months ago

With Janeway 1.5, when adding a chapter to a book, I get TypeError: add() got an unexpected keyword argument 'through_defaults' (full traceback below).

I suspect its related to the upgrade to Django 3.2.

Internal Server Error: /plugins/books/admin/edit/2/chapter/new/
Traceback (most recent call last):
  File "...django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "...django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "...django/contrib/auth/decorators.py", line 21, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "./plugins/books/views.py", line 370, in books_chapter
    form.save_m2m()
  File "...django/forms/models.py", line 451, in _save_m2m
    f.save_form_data(self.instance, cleaned_data[f.name])
  File "...django/db/models/fields/related.py", line 1668, in save_form_data
    getattr(instance, self.attname).set(data)
  File "...django/db/models/fields/related_descriptors.py", line 1015, in set
    self.add(*new_objs, through_defaults=through_defaults)
TypeError: add() got an unexpected keyword argument 'through_defaults'
gamboz commented 10 months ago

Rewriting M2MOrderedThroughManager.add as follows seems to fix the issue (just empirically, I don't really know what's going on :)

       def add(self, *objs, through_defaults=None):
            with allow_m2m_operation(rel.through):
                return super().add(*objs, through_defaults=through_defaults)