English 中文(简体)
例外:无
原标题:Exception : Does Not Exist

I am trying to fill in the DB table " Notification " through a function as follows :

我的模式:

 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 )

并且正在利用这一职能,在其它评估中将“通知”发送到:

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: 
最佳回答

在您的寄送中——作为通知——你需要指定通知小组,了解你通知程序的内容价值:

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

The same will have to be done for NotificationType on Notification.

One piece of advice I d like to give you:

The fields that you ve named with _id on the end (e.g. content_id, notification_type_id) are not storing the id s, they are pointers to the actual objects!!! This means that not only will that model have those fields, but django should (I think) also create the following two fields, which actually do point to the id s of the objects in question: content_id_id, notification_type_id_id.

Very bad, you should just name it after the Model itself, so: content, notification_type.

问题回答

根据追溯,它所看到的是,在寄送的“通知”中,一些事情正在破碎,其行文如下:

notification.content_id.link_id = link_id

根据你的代码样本,我可以假设你重新使用模型,尽管我可以说明正在探讨的模式。 检查你在数据库中实际上有通知自动浏览量,链接是你回过来的。

In the definition of your Notification model you want to reference the NotificationContent model. You ve done this by citing content_id as a foreign key.

但是,“属性”(content_id)更名为content,因为要求属性将退回模型的一个实例,而不仅仅是识别特征。

notification.content_id.link_id = link_id 

出现错误的原因是,你把系统识别资料直接转而处理,而不是让djangos ORM处理。 包括:在物体上通过,而不是复制......

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

You might find ContentTypes and signals directly applicable to your problem.





相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签