pythondjangodjango-modelsdjango-packagesdjango-markdownx

Django model does not recognize MarkdownxFormField field


I am trying to add markdownx support to my model, which will enable preview editing from the admin panel. However, once i change my content field from models.FileField to MarkdownXFromField() django just deletes the content field when migrating and ignores it as if it wasn't part of the model at all.

I've followed these docs exactly but it's not working.

I have also ran collectstatic.

# models.py

from os.path import splitext
from uuid import uuid4
from django.db import models
from markdownx.fields import MarkdownxFormField

def hashImageFilename(instance, name):
    ext = splitext(name)[1]
    return "images/{}{}".format(uuid4(), ext)


class Article(models.Model):
    title = models.CharField(("title"), max_length=100)
    content = MarkdownxFormField()
    description = models.TextField(("description"), default='')
    uploadDate = models.DateTimeField(("uploadDate"), auto_now=True)
    lastModified = models.DateTimeField(("uploadDate"), auto_now=True)
    publicationDate = models.DateField("publicationDate")
    image = models.ImageField("image", upload_to=hashImageFilename)

    def __str__(self):
        return self.title
# urls.py

from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from django.conf.urls import url
from django.conf import settings
from markdownx import urls as markdownx

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api-auth/', include('rest_framework.urls')),
    path('api/articles/', include('articles.api.urls')),
    url(r'^markdownx/', include('markdownx.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
# admin.py

from django.contrib import admin

# Register your models here.
from markdownx.admin import MarkdownxModelAdmin
from .models import Article

admin.site.register(Article, MarkdownxModelAdmin)
# settings.py

INSTALLED_APPS = [
  #...
  'markdownx'
]

Solution

  • You are confusing the MarkdownxFormField form field with the MarkdownxField model field. You should rewrite the model to:

    # models.py
    
    from os.path import splitext
    from uuid import uuid4
    from django.db import models
    from markdownx.models import MarkdownxField
    
    def hashImageFilename(instance, name):
        ext = splitext(name)[1]
        return "images/{}{}".format(uuid4(), ext)
    
    
    class Article(models.Model):
        title = models.CharField(("title"), max_length=100)
        content = MarkdownxFormField()
        description = models.TextField(("description"), default='')
        uploadDate = models.DateTimeField(("uploadDate"), auto_now=True)
        lastModified = models.DateTimeField(("uploadDate"), auto_now=True)
        publicationDate = models.DateField("publicationDate")
        image = models.ImageField("image", upload_to=hashImageFilename)
    
        def __str__(self):
            return self.title

    The MarkdownxFormField is used for forms, it will thus render with a specific widget, etc. In order to store content in the database, you need a model field.