例外:不存在

时间:2011-05-10 14:53:02

标签: django function view

我试图通过以下函数填写DB表“Notification”:

我的模特:

 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: 

3 个答案:

答案 0 :(得分: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。

非常糟糕,你应该在Model本身之后命名,所以:content,notification_type。

答案 1 :(得分:0)

基于回溯,看起来像是在行的send_as_notification_to函数中打破了:

notification.content_id.link_id = link_id

根据您的代码示例,我可以假设您正在使用模型,但我无法确定实例化的模型。检查您实际上是否在数据库中有一个NotificationContent行,其中包含您传入的link_id。

答案 2 :(得分:0)

Notification模型的定义中,您想引用NotificationContent模型。您已通过引用content_id作为外键来完成此操作。

但是,content_id属性更好地命名为content,因为调用该属性将返回模型的实例,而不仅仅是标识符。

notification.content_id.link_id = link_id 

返回错误,因为你直接搞乱系统标识符而不是让django的ORM处理它。例如:传入对象,而不是id ......

def send_as_notification_to(obj...):
    notification.content = obj

您可能会发现ContentTypessignals直接适用于您的问题。