null
和
blank
设置为True。问题是,当我从管理界面编辑一个类别时,我不能将ForeignKey更改为
---------
(这看起来像管理员说无的方式。)值最初可以是无,我可以通过管理员将其更改为实际值,我可以更改为另一个值,但一旦它具有实际值,我就不能将其更改回无。(即通过管理员)
为什么会这样?
这是你的密码
models.py
from django.db import models
import tinymce.models
from photologue.models import Photo
from django.utils.translation import ugettext_lazy as _
import multilingual
class SimplePage(models.Model):
slug = models.SlugField(
_('Slug'),
unique=True,
help_text=_('''Unique identifier for URL. Only letters, digits, and -.\
e.g. history-oct-2000 or about''')
)
category = models.ForeignKey('Category',
related_name='items_including_main_page',
blank=True, null=True)
class Translation(multilingual.Translation):
title = models.CharField(_('Title'), max_length=42)
content = tinymce.models.HTMLField(_('Content'), blank=True)
class Admin:
list_display = ('title',)
search_fields = ('title', 'content')
class Meta:
verbose_name = _('Simple page')
verbose_name_plural = _('Simple pages')
__unicode__ = lambda self: self.title
class Category(models.Model):
main_page = models.OneToOneField(
SimplePage,
related_name='_SimplePage__category_which_has_this_as_title',
blank=True,
null=True)
get_title = lambda self: self.main_page.title if self.main_page else u''
get_items = lambda self: \
self.items_including_main_page.exclude(id__exact=self.main_page.id)
__unicode__ = lambda self: self.get_title() or u'Titleless Category'
class Admin:
pass
class Meta:
verbose_name = _('Category')
verbose_name_plural = _('Categories')
以及
admin.py
:
from sitehelpers.models import *
from django.contrib import admin
import multilingual
class SimplePageAdmin(multilingual.ModelAdmin):
pass
admin.site.register(SimplePage, SimplePageAdmin)
admin.site.register(Category)