2011-05-10 24 views
0

我想通過一個函數來填寫數據庫表「通知」如下:例外:不存在

我的模型:

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: 
+0

有沒有人可以幫助我? – 2011-05-10 15:38:09

回答

1

在你send_as_notification_to功能,你需要一個NotificationContent實例分配給您的通知實例的content_id值:

nc = NotificationContent.objects.create(link_id=link_id, unique_content= unique_content) 
notification = Notification(person = person) 
notification.content_id = nc 
... 

通知上的NotificationType也必須完成。

一個建議的一塊,我想給你:

您與_id上月底命名(如的content_id,notification_type_id)不存儲的ID字段,它們指向實際對象!這意味着該模型不僅具有這些字段,而且django應該(我認爲)還創建以下兩個字段,其實際上指向有問題的對象的id:content_id_id,notification_type_id_id。

非常糟糕,您應該在模型本身後面將其命名爲:content,notification_type。

0

基於它看起來像東西就行了send_as_notification_to功能讀取被打破了回溯:基於您的代碼樣本

notification.content_id.link_id = link_id 

我可以假設你儘管我不能說出正在實例化的模型,但是使用了模型。檢查你確實有一個NotificationContent行中與你傳遞的link_id數據庫

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直接適用於您的問題。