例外:不存在
Posted
技术标签:
【中文标题】例外:不存在【英文标题】:Exception : Does Not Exist 【发布时间】:2011-05-10 14:53:01 【问题描述】:我正在尝试通过如下函数填写数据库表“通知”:
我的模型:
class NotificationType(models.Model):
type = models.CharField(max_length = 100)
application = models.CharField(max_length = 100)
description = models.CharField(max_length = 1000 , null = True)
class NotificationContent(models.Model):
link_id = models.IntegerField()
unique_content = models.CharField(max_length = 500)
class Notification(models.Model):
person = models.ForeignKey(User)
content_id = models.ForeignKey(NotificationContent)
notification_type_id = models.ForeignKey(NotificationType)
datetime = models.DateTimeField(auto_now_add = True)
is_active = models.BooleanField(default = 1)
read_unread = models.BooleanField( default = 0 )
并在其他应用程序视图中使用函数 send_as_notification_to() 作为:
def crave_form(request):
if request.method == 'POST':
form = IcraveForm(request.POST)
if form.is_valid():
crave = form.save(commit = False)
crave.person = request.user
crave.save()
send_as_notification_to( crave.person , crave.id , crave.person , 'icrave' , 'crave' )
else:
form = IcraveForm()
return render(request, 'icrave/form.html', 'form' : form)
函数定义:
def send_as_notification_to(person , link_id , unique_content , which_app, notification_type ):
notification = Notification(person = person)
notification.content_id.link_id = link_id
notification.content_id.unique_content = unique_content
notification.notification_type_id.type = notification_type
notification.notification_type_id.application = which_app
追溯:
Environment:
Request Method: POST
Request URL: http://localhost:8000/icrave/create/
Django Version: 1.3
Python Version: 2.7.1
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.comments',
'ec.kiosk',
'ec.chakra',
'ec.ajax',
'ec.broadcast',
'ec.connect',
'ec.seek',
'ec.feed',
'ec.ec_model',
'ec.info',
'ec.domains',
'ec.souk',
'ec.meta',
'ec.shastra',
'ec.chat',
'ec.log',
'ec.icrave',
'ec.notification',
'django.contrib.admin']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/Volumes/Disk2/workspace/ec/ec/icrave/views.py" in crave_form
16. send_as_notification_to( crave.person , crave.id , crave.person , 'icrave' , 'crave' )
File "/Volumes/Disk2/workspace/ec/ec/notification/api.py" in send_as_notification_to
6. notification.content_id.link_id = link_id
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/fields/related.py" in __get__
301. raise self.field.rel.to.DoesNotExist
Exception Type: DoesNotExist at /icrave/create/
Exception Value:
【问题讨论】:
有没有人可以帮我解决这个问题?? 【参考方案1】:在您的 send_as_notification_to 函数中,您需要将 NotificationContent 实例分配给您的 Notification 实例的 content_id 值:
nc = NotificationContent.objects.create(link_id=link_id, unique_content= unique_content)
notification = Notification(person = person)
notification.content_id = nc
...
Notification 上的 NotificationType 也必须这样做。
我想给你一个建议:
您最后用 _id 命名的字段(例如 content_id、notification_type_id)不存储 id,它们是指向实际对象的指针!!!这意味着该模型不仅将具有这些字段,而且 django 应该(我认为)还创建以下两个字段,实际上 确实 指向相关对象的 id:content_id_id、notification_type_id_id。
非常糟糕,你应该在模型本身之后命名它,所以:内容,通知类型。
【讨论】:
【参考方案2】:根据回溯,看起来在 send_as_notification_to 函数中出现了以下内容:
notification.content_id.link_id = link_id
根据您的代码示例,我可以假设您正在使用模型表单,但我无法确定正在实例化的模型。检查您在数据库中是否确实有一个 NotificationContent 行,其中包含您传入的 link_id。
【讨论】:
【参考方案3】:在Notification
模型的定义中,您要引用NotificationContent
模型。您通过引用 content_id
作为外键来完成此操作。
不过,content_id
属性最好直接命名为 content
,因为调用该属性将返回模型的实例,而不仅仅是标识符。
notification.content_id.link_id = link_id
返回错误,因为您直接弄乱了系统标识符,而不是让 django 的 ORM 处理它。 eg:传入对象,而不是id...
def send_as_notification_to(obj...):
notification.content = obj
您可能会发现ContentTypes 和signals 直接适用于您的问题。
【讨论】:
以上是关于例外:不存在的主要内容,如果未能解决你的问题,请参考以下文章