Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeyError in a del #132

Closed
fpoulain opened this issue Aug 21, 2020 · 8 comments
Closed

KeyError in a del #132

fpoulain opened this issue Aug 21, 2020 · 8 comments

Comments

@fpoulain
Copy link

Hi,

I encountered a KeyError in a del:

Traceback (most recent call last):
  File "venv/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "venv/lib/python3.7/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "venv/lib/python3.7/site-packages/wagtail/admin/urls/__init__.py", line 105, in wrapper
    return view_func(request, *args, **kwargs)
  File "venv/lib/python3.7/site-packages/wagtail/admin/auth.py", line 165, in decorated_view
    return view_func(request, *args, **kwargs)
  File "venv/lib/python3.7/site-packages/wagtail/contrib/modeladmin/options.py", line 352, in create_view
    return view_class.as_view(**kwargs)(request)
  File "venv/lib/python3.7/site-packages/django/views/generic/base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "venv/lib/python3.7/site-packages/wagtail/contrib/modeladmin/views.py", line 614, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "venv/lib/python3.7/site-packages/django/utils/decorators.py", line 45, in _wrapper
    return bound_method(*args, **kwargs)
  File "venv/lib/python3.7/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "venv/lib/python3.7/site-packages/wagtail/contrib/modeladmin/views.py", line 73, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "venv/lib/python3.7/site-packages/django/views/generic/base.py", line 97, in dispatch
    return handler(request, *args, **kwargs)
  File "venv/lib/python3.7/site-packages/django/views/generic/edit.py", line 142, in post
    return self.form_valid(form)
  File "venv/lib/python3.7/site-packages/wagtail/contrib/modeladmin/views.py", line 168, in form_valid
    instance = form.save()
  File "venv/lib/python3.7/site-packages/modelcluster/forms.py", line 373, in save
    formset.save(commit=commit)
  File "venv/lib/python3.7/site-packages/modelcluster/forms.py", line 118, in save
    manager.commit()
  File "venv/lib/python3.7/site-packages/modelcluster/fields.py", line 210, in commit
    del self.instance._cluster_related_objects[relation_name]

I am using :
Django 2.2.15
django-modelcluster 5.0.2
wagtail 2.7.4

@gasman
Copy link
Contributor

gasman commented Aug 21, 2020

Please can you provide steps to reproduce this?

@fpoulain
Copy link
Author

Its a bit tricky but for (still) unknown reasons my code don't appears in the stack.

I have a model Event (inherited from wagtail Page) and a model EventRange related via a parental key to Event.

I overridden Event.save in order to fill begin/end from its related ranges. But it didn't worked properly (self.ranges was empty). So I reversed the logic: overriding EventRange.save() to update and save (only when needed) the Event object.

Modulo avoiding the KeyError in my venv, the reversed logic worked as expected.

@gasman
Copy link
Contributor

gasman commented Aug 21, 2020

Will close for now - happy to reopen if you can provide full steps to reproduce.

@gasman gasman closed this as completed Aug 21, 2020
@fpoulain
Copy link
Author

I did it there : https://forge.cliss21.org/fpoulain/issue132

you should:

  • clone & cd
  • make init
  • $editor will edit config.env then set ENV=development
  • create superuser and try to add an Event via /admin/base/event/create/.

@gasman gasman reopened this Aug 21, 2020
@fpoulain
Copy link
Author

I also added a branch why_dont_that_work that illustrate my first interrogation. The same logic doesn't works on the clusterable model but works as expected on the Page model. I didn't found any clue by reading wagtail/core/models.py
(I don't have strong idea about if it's another issue)

@fpoulain
Copy link
Author

fpoulain commented Sep 3, 2020

This bug is quite blocking for me. Is there anyway I can help?

@fpoulain
Copy link
Author

fpoulain commented Sep 3, 2020

I also added a branch why_dont_that_work that illustrate my first interrogation. The same logic doesn't works on the clusterable model but works as expected on the Page model. I didn't found any clue by reading wagtail/core/models.py

Looking it closer I found why. So I share it.

By using WagtailModelAdmin, the Create/Edit form inherit from ClusterForm and in it's save() method the parent and the related are saved sequentially as usually with django related inline forms. So that normal that in the model's save method the child objects aren't accessible. Here we have to catch form's save method and not model's save method.

I didn't seen ClusterForm at first stuff because genericity of WMA makes it a bit tricky. And modelcluster docs don't suggest the existence of the ClusterForm mixin.

Thanks you so long for all wagtail.

@gasman
Copy link
Contributor

gasman commented Sep 3, 2020

Glad you got it sorted! Yes, I think the custom logic on EventRange.save is what's causing the problems here - I don't think there's really anything that can be done in django-modelcluster to avoid that.

@gasman gasman closed this as completed Sep 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants