gpt4 book ai didi

python - Django 模型无法识别 MarkdownxFormField 字段

转载 作者:行者123 更新时间:2023-12-04 10:43:08 24 4
gpt4 key购买 nike

我正在尝试添加 markdownx支持我的模型,这将允许从管理面板进行预览编辑。然而,一旦我改变了我的 content字段来自 models.FileFieldMarkdownXFromField() django 只是在迁移时删除 content 字段并忽略它,就好像它根本不是模型的一部分一样。

我已关注 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'
]

最佳答案

你在混淆 MarkdownxFormField带有 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用于表单,因此它将使用特定的小部件等呈现。为了将内容存储在数据库中,您需要一个模型字段。

关于python - Django 模型无法识别 MarkdownxFormField 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59841120/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com