-
Notifications
You must be signed in to change notification settings - Fork 67
Copy/pasting a Form/Fieldset/Field plugins through CMS copy & paste interface causes a KeyError #167
Comments
I had the same issue just one day ago, now I know why it happened! |
Thank you for the reply. Sorry for the delay in responding. 🙂 I'm a bit new to Django CMS. How would I go about deleting a plugin? Are you referring going into the database and removing the plugin there? Or is there a management layer for doing this (such as manage.py or the web UI)? |
The easiest approach for this would be to use the python interactive interpreter of django. There you can import the model and fetch an instance from your DB. When you got it and checked if it is the one you want to delete you can use
|
I have stepped into this error as well. Would a stacktrace help to fix the error? |
I haven't had time to investigate further, but I think the problem is that the copy/paste function (insofar as I currently understand it) is duplicating the field name--and that duplication is causing the issue; two fields in the same 'node' in the tree with the same name. Hope that helps. When time allows, I'll spend more time looking into the root cause of that issue--what I mentioned is based on my observations of the problem in my environment, so that may or may not be on the right track. |
Turns out I raised a duplicate of this ticket - #197, I'm posting the most important data from it below:
Last part of the stack trace:
|
I thought about it as well, but not sure anymore since the whole page can be copied normally. The stacktrace indicates that the problem occurs because aldryn-forms notices that the form cache Here's the whole method, it fails on the last line: def get_form_field_name(self, field):
if self._form_field_key_cache is None:
self._form_field_key_cache = {}
if field.pk not in self._form_field_key_cache:
fields_by_key = self.get_form_fields_by_name()
for name, _field in fields_by_key.items():
self._form_field_key_cache[_field.plugin_instance.pk] = name
return self._form_field_key_cache[field.pk] |
Here's a breakdown of what happens in that piece of code def get_form_field_name(self, field: Field) -> str:
if self._form_field_key_cache is None:
self._form_field_key_cache = {}
# true
if field.pk not in self._form_field_key_cache:
fields_by_key = self.get_form_fields_by_name()
# field.pk = 10
# fields_by_key doesn't contain the required field, hence it's never added to self._form_field_key_cache
for name, _field in fields_by_key.items():
self._form_field_key_cache[_field.plugin_instance.pk] = name
return self._form_field_key_cache[field.pk] |
I noticed that this issue affects copying of any individual field as well (at least for email and text fields). |
The problem lies in the code that dynamically builds a Form class or collects Form fields - it accumulates the fields in a dict using Field.name as a key, since Field.name doesn't enforce uniqueness in any manner a lot of places either ignore the duplicates or raise errors. |
I pushed a fix to the master branch and will be testing on a project that's going to go live in ~2 weeks. |
A related bug remains though - fields with identical names are being merged into one field, eg Form submission:
Is going to be saved as:
|
I created 5.0.5 release which can be installed by adding to requirements.txt as:
As I don't have permissions to push it to pypi we have to use github for now. The name uniqueness issue we can track in #207 |
Technical analysis report from @victor-yunenko:
Original briefing by theitsmith
I was creating a contact form in Structure mode. To create a Bootstrap form (perhaps there's a better way?), I wrapped the form fields in a div to apply the appropriate Bootstrap classes--this worked well for the first form field.
To add the second, I copied the div structure of the first field and pasted into the appropriate place in the form--the plan was then to edit that newly pasted field and change a couple of attributes (such as 'id', 'name', 'placeholder', etc. However, immediately after pasting the form field, that page now errors with the following (while viewing in with DEBUG=True):
Now the page won't load at all in Edit mode, it just generates that error.
Two questions:
The text was updated successfully, but these errors were encountered: