Py学习  »  Django

Django模型无法识别MarkdownxFormField字段

aviya.developer • 6 年前 • 2663 次点击  

我想补充一下 markdownx content 字段来自 models.FileField 到 MarkdownXFromField() django只是在迁移时删除内容字段,并忽略它,就好像它根本不是模型的一部分一样。

我已经跟踪了 these docs

我也跑过 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'
]
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/53370
文章 [ 1 ]  |  最新文章 6 年前
Willem Van Onsem
Reply   •   1 楼
Willem Van Onsem    6 年前

你在混淆 Markdownx Form Field 形式 与 MarkdownxField 模型字段。您应该将模型重写为:

# 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

这个 MarkdownxFormField 用于 形式 ,因此它将使用特定的小部件等呈现。为了在数据库中存储内容,您需要一个模型 .