我知道标题令人困惑,但让我解释一下。我目前正在为django + django-cms项目实现一个小的wiki apphook。我正在尝试向维基添加部分,维基页面可以存在的部分是特定的部分,允许一些结构。
这是我的model.py
:
from django.db import models
from djangocms_text_ckeditor.fields import HTMLField
class WikiPage(models.Model):
slug = models.SlugField(max_length=50,primary_key=True)
name = models.CharField(max_length=50)
content = HTMLField(blank=True)
section = models.ForeignKey('WikiSection', related_name='pages', db_index=True)
def __str__(self):
return self.name
class WikiSection(models.Model):
slug = models.SlugField(max_length=50, primary_key=True)
name = models.CharField(max_length=50)
def __str__(self):
return self.name
这是我的admin.py
:
from django.contrib import admin
from .models import WikiPage, WikiSection
from django import forms
from forms import WikiPageForm, WikiSectionForm
class WikiPageAdmin(admin.ModelAdmin):
form = WikiPageForm
admin.site.register(WikiPage, WikiPageAdmin)
class WikiSectionAdmin(admin.ModelAdmin):
form = WikiSectionForm
admin.site.register(WikiSection, WikiSectionAdmin)
忽略了可怕的重要性,我知道。
这是我的forms.py
:
from django import forms
from .models import WikiPage, WikiSection
class WikiPageForm(forms.ModelForm):
class Meta:
model = WikiPage
exclude = ['slug']
def clean(self):
cleaned_data = super(WikiPageForm, self).clean()
print cleaned_data
class WikiSectionForm(forms.ModelForm):
class Meta:
model = WikiSection
exclude = ['slug']
现在,当我尝试填写字段并保存管理表单时,它总是告诉我部分字段是必需的,即使我填写了字段。我也意识到clean()方法中没有该字段。这是它打印的内容:
{'content': u'<p>sdfsdf</p>\r\n', 'name': u'fdfsdf'}
修改 我也检查了数据并没有在POST中发送
答案 0 :(得分:0)
models.py
:
from django.db import models
from django.utils.text import slugify
from djangocms_text_ckeditor.fields import HTMLField
class WikiPage(models.Model):
slug = models.SlugField(max_length=50,primary_key=True)
name = models.CharField(max_length=50)
content = HTMLField(blank=True)
section = models.ForeignKey('WikiSection', related_name='pages', db_index=True)
def __str__(self):
return self.name
<!-- ADDED THIS -->
def save(self, *args, **kwargs):
self.slug = slugify(self.name)
super(WikiPage, self).save(*args, **kwargs)
<!-- ADDED THIS -->
class WikiSection(models.Model):
slug = models.SlugField(max_length=50, primary_key=True)
name = models.CharField(max_length=50)
def __str__(self):
return self.name
<!-- AND THIS -->
def save(self, *args, **kwargs):
self.slug = slugify(self.name)
super(WikiSection, self).save(*args, **kwargs)
<!-- AND THIS -->