本文介绍了Django:无法将ForeignKey值设置为None的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型类别,它具有一个ForeignKey到SimplePage模型。 null blank 设置为True。问题是,当我从管理界面编辑类别时,我不能将ForeignKey更改为 --------- (这看起来像管理员的说出无的方法)最初的值可以是None,我可以通过管理员把它改变为一个实际的值,我可以改变到另一个值,但一旦有了实际值,我就不能把它改回到None 。 (通过管理员,那是。)



为什么是这样?



更新: strong>



以下是 models.py 的代码:



来自django.db导入模型
import tinymce.models
from photologue.models import照片

from django.utils.translation导入ugettext_lazy为_
导入多语言

类SimplePage(models.Model):

slug = models.SlugField(
_('Slug') ,
unique = True,
help_text = _('''URL的唯一标识符只有字母,数字和-.\
例如history-oct-2000或about''' )


category = models.ForeignKey('Category',
related_name ='items_including_main_page',
blank = True,null = True)

class Translation(multilingual.Translation):
title = models.CharField(_('Title'),max_len gth = 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类别(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()或u'Titleless类别'

class Admin:
pass

class Meta:
verbose_name = _('Category')
verbose_name_plural = _('Categories')

admin.py

 code> from sitehelpers.models import * 
from django.contrib import admin
import多语言

class SimplePageAdmin(multilingual.ModelAdmin):
pass

admin.site.register(SimplePage,SimplePageAdmin)

admin.site.register(类别)


解决方案

也许这是因为您在类别中也定义了OneToOne关系,因此不能断开此关系。尝试删除它并查看,如果您可以将SimplePage中的类别设置为无。


I have a model Category which has a ForeignKey to a SimplePage model. null and blank are set to True. The problem is, when I edit a Category from the admin interface, I can't change the ForeignKey to --------- (Which looks like the admin's way of saying None.) The value can be None initially, and I can change it to an actual value through the admin, and I can change to another value, but once it has an actual value I can't change it back to None. (Through the admin, that is.)

Why is this?

UPDATE:

Here's the code for 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')

And 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)
解决方案

Maybe that's because you have OneToOne relationship defined also in category and you can't therefore break this relationship. Try to remove it and see, if you can set category in SimplePage to None.

这篇关于Django:无法将ForeignKey值设置为None的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 11:23