Updating an existing Snippet results in invalid error message
When you update an existing snippet, the admin will throw an error message "A Snippet with this name already exists."
Looks like the issue is the clean method, it literally checks if that snippet exists already. This makes sense for new snippets, but not for updating existing ones.
https://github.com/django-cms/djangocms-snippet/blob/master/src/djangocms_snippet/forms.py#L37-L46
Steps to reproduce:
- Go to Admin > Snippets > Snippet
- Add a new Snippet
- Add any name and HTML content
- Hit "Save and continue editing"
- Hit "Save and continue editing" again
I fixed this on my side now by monkeypatching the clean method.
class BetterSnippetForm(SnippetForm):
def clean(self) -> dict:
return self.cleaned_data
I think it's better to rely on a unique=True property in the name/slug Model field rather than trying to fix it within the clean method. Though that might not be backwards compatible.
@bartTC Thank you for raising this issue.
Restricting this on DB-Level will not work since snipets can be versioned in principle and there might be several versions of the same snippet.
Looking at the source, I come to think that the lines
https://github.com/django-cms/djangocms-snippet/blob/62bb317e68f1274ee8b1091466604ce763d9a130/src/djangocms_snippet/forms.py#L39-L40
might be the issue. I believe the exclude should always be applied to the queryset. Am I right in assuming that you do not use versioned snippets?
That's correct, I'm not using versioning. It's used in a Django CMS 3.x environment.
Would you mind testing the branch #177 to see if it works for you?
I'll check, might take until Friday though.
@fsbraun this is fixing it! Thank you 👍