Django 模型无法识别 MarkdownxFormField 字段
Django model does not recognize MarkdownxFormField field
我正在尝试为我的模型添加 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'
]
您混淆了 Markdownx<b>Form</b>Field
form 字段与 MarkdownxField
模型字段。您应该将模型重写为:
# models.py
from os.path import splitext
from uuid import uuid4
from django.db import models
from markdownx<b>.models</b> import <b>MarkdownxField</b>
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 = <b>MarkdownxFormField()</b>
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
用于 forms,因此它将使用特定的小部件等呈现。为了将内容存储在数据库中,您需要一个模型字段.
我正在尝试为我的模型添加 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'
]
您混淆了 Markdownx<b>Form</b>Field
form 字段与 MarkdownxField
模型字段。您应该将模型重写为:
# models.py
from os.path import splitext
from uuid import uuid4
from django.db import models
from markdownx<b>.models</b> import <b>MarkdownxField</b>
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 = <b>MarkdownxFormField()</b>
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
用于 forms,因此它将使用特定的小部件等呈现。为了将内容存储在数据库中,您需要一个模型字段.