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

Compatibility with django-ckeditor-5 (support of the 'CKEditor5Field' field type) #576

Open
mikeromanov opened this issue Nov 29, 2020 · 3 comments

Comments

@mikeromanov
Copy link

Hello! My website uses django-modeltranslation to support different languages. Also I want to use django-ckeditor-5 for generate rich text content. However, on trying to attach django-ckeditor-5 to my site, I got the following error:

django.core.exceptions.ImproperlyConfigured: CKEditor5Field is not supported by modeltranslation.

As I understand, the django-modeltranslation doesn't work with the field type 'CKEditor5Field'.

Is it possible to add support of CKEditor5Field field type to django-modetranslation?

@dmenisdev
Copy link

dmenisdev commented Feb 11, 2021

Instead of using the CKEditor5Field in your model use the widget in your Form.
Set your model field (back) to a TextField

If you want to use it in Django-admin override the form field in the admin.py:

from django_ckeditor_5.widgets import CKEditor5Widget

class [Your Model Name]Admin(ImportExportModelAdmin, admin.ModelAdmin):
  formfield_overrides = {
      models.TextField: {'widget': CKEditor5Widget},
  }

Hope this answers your question.

@hvlads useful to put in the readme? Happy to do so if you like :)

@Hopiu
Copy link
Contributor

Hopiu commented Jun 2, 2023

To use the CKEditor5Field with django-modeltranslation, you can add it to the MODELTRANSLATION_CUSTOM_FIELDS setting. This will add support for the field to be translated in the model.

Take a look at the documentation here https://django-modeltranslation.readthedocs.io/en/latest/installation.html#modeltranslation-custom-fields

@PublisherName
Copy link

Using django-ckeditor-5 in Django Admin for specific textfields.

Using this approach, you don’t have to worry about modeltranslation errors related to unsupported fields. Additionally, there’s no need to add MODELTRANSLATION_CUSTOM_FIELDS in the settings, as everything is stored as text.

  1. Define a custom ModelAdmin that applies the CKEditor5 widget to specific TextField fields:
# base_admin.py
from django.contrib import admin
from django.db import models
from django_ckeditor_5.widgets import CKEditor5Widget

class CKEditorModelAdmin(admin.ModelAdmin):
    ckeditor_fields = []

    def formfield_for_dbfield(self, db_field, request=None, **kwargs):
        if (
            isinstance(db_field, models.TextField)
            and db_field.name in self.ckeditor_fields
        ):
            kwargs["widget"] = CKEditor5Widget()
        return super().formfield_for_dbfield(db_field, request, **kwargs)
  1. Here’s a random model with a TextField where you want to integrate the CKEditor5 widget:
# models.py
from django.db import models

class RandomModel(models.Model):
    title = models.CharField(max_length=255)
    description = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title
  1. Use the Custom CKEditorModelAdmin in admin.py
# admin.py
from django.contrib import admin
from .base_admin import CKEditorModelAdmin
from .models import RandomModel

@admin.register(RandomModel)
class RandomModelAdmin(CKEditorModelAdmin):
    list_display = ("title", "description", "created_at")
    ckeditor_fields = ("description",)

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

4 participants