2

我将 Tastypie 与 neo4django 一起使用。我关注https://gist.github.com/mhluongo/5789513http://django-tastypie.readthedocs.org/en/latest/interacting.html。现在,在(新)提交的 POST 或 PUT 时,我坚持夸大某些关系的价值(通过 resource_uri?!)。“'unicode' 对象没有属性 'pk'”。

提交.py

from neo4django.db import models

from core.models.user import AppUser
from core.models.slide import Slide

class Submission(models.NodeModel):

    value = models.ArrayProperty()
    started = models.DateTimeProperty()
    finished = models.DateTimeProperty()

    user = models.Relationship(AppUser,
                                rel_type='answered_by',
                single=True,
                                related_name='submissions'
                               )

    slide = models.Relationship(Slide,
                                rel_type='response_to',
                single=True,
                                related_name='submissions'
                               )

api.py

class UserResource(ModelResource):
    class Meta:
        queryset = AppUser.objects.all()
        resource_name = 'user'
        excludes = ['password']
        allowed_methods = ['get']
        authentication = BasicAuthentication()

class SlideResource(ModelResource):
    class Meta:
        queryset = Slide.objects.all()
        resource_name = 'slide'
        excludes = ['answers', 'require_order', 'require_all']
        authorization = Authorization()

class SubmissionResource(ModelResource):
    class Meta:
        queryset = Submission.objects.all()
        resource_name = 'submission'
        excludes = ['require_order', 'require_all']
        authorization = Authorization()    

网址.py

from django.conf.urls import patterns, include, url

from api.api import *

user_resource = UserResource()
slide_resource = SlideResource()
submission_resource = SubmissionResource()

urlpatterns = patterns('',

    url(r'^api/', include(user_resource.urls)),
    url(r'^api/', include(slide_resource.urls)),
    url(r'^api/', include(submission_resource.urls)),
)

curl .../提交/模式/

{"allowed_detail_http_methods": ["get", "post", "put", "delete", "patch"], "allowed_list_http_methods": ["get", "post", "put", "delete", "patch"], "default_format": "application/json", "default_limit": 20, "fields": {"finished": {"blank": false, "default": "No default provided.", "help_text": "A date & time as a string. Ex: \"2010-11-10T03:07:43\"", "nullable": true, "readonly": false, "type": "datetime", "unique": false}, "id": {"blank": true, "default": "", "help_text": "Integer data. Ex: 2673", "nullable": false, "readonly": false, "type": "integer", "unique": true}, "resource_uri": {"blank": false, "default": "No default provided.", "help_text": "Unicode string data. Ex: \"Hello World\"", "nullable": false, "readonly": true, "type": "string", "unique": false}, "slide": {"blank": true, "default": "", "help_text": "Unicode string data. Ex: \"Hello World\"", "nullable": false, "readonly": false, "type": "string", "unique": false}, "started": {"blank": false, "default": "No default provided.", "help_text": "A date & time as a string. Ex: \"2010-11-10T03:07:43\"", "nullable": true, "readonly": false, "type": "datetime", "unique": false}, "user": {"blank": true, "default": "", "help_text": "Unicode string data. Ex: \"Hello World\"", "nullable": false, "readonly": false, "type": "string", "unique": false}, "value": {"blank": false, "default": "No default provided.", "help_text": "Unicode string data. Ex: \"Hello World\"", "nullable": true, "readonly": false, "type": "string", "unique": false}}}

错误:

HTTP/1.1 500 INTERNAL SERVER ERROR
Server: nginx/1.1.19
Date: Sat, 09 Nov 2013 15:20:35 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive

{"error_message": "'unicode' object has no attribute 'pk'",
"traceback": "Traceback (most recent call last):\n\n  File \"/opt/phaidra/env/local/lib/python2.7/site-packages/tastypie/resources.py\", line 195,
in wrapper\n    response = callback(request, *args, **kwargs)\n\n  File \"/opt/phaidra/env/local/lib/python2.7/site-packages/tastypie/resources.py\", line 435,
in dispatch_detail\n    return self.dispatch('detail', request, **kwargs)\n\n  File \"/opt/phaidra/env/local/lib/python2.7/site-packages/tastypie/resources.py\", line 458,
in dispatch\n    response = method(request, **kwargs)\n\n  File \"/opt/phaidra/env/local/lib/python2.7/site-packages/tastypie/resources.py\", line 1408,
in put_detail\n    updated_bundle = self.obj_update(bundle=bundle, **self.remove_api_resource_names(kwargs))\n\n  File \"/opt/phaidra/env/local/lib/python2.7/site-packages/tastypie/resources.py\", line 2144,
in obj_update\n    return self.save(bundle, skip_errors=skip_errors)\n\n  File \"/opt/phaidra/env/local/lib/python2.7/site-packages/tastypie/resources.py\", line 2230,
in save\n    bundle.obj.save()\n\n  File \"/opt/phaidra/env/src/neo4django/neo4django/db/models/base.py\", line 315,
in save\n    return super(NodeModel, self).save(using=using, **kwargs)\n\n  File \"/opt/phaidra/env/local/lib/python2.7/site-packages/django/db/models/base.py\", line 546,
in save\n    force_update=force_update, update_fields=update_fields)\n\n  File \"/opt/phaidra/env/src/neo4django/neo4django/db/models/base.py\", line 333,
in save_base\n    self._save_neo4j_relationships(self, self.__node)\n\n  File \"/opt/phaidra/env/src/neo4django/neo4django/db/models/relationships.py\", line 333,
in _save_\n    rels[key]._save_relationship(instance, node, state[key])\n\n  File \"/opt/phaidra/env/src/neo4django/neo4django/db/models/relationships.py\", line 499,
in _save_relationship\n    rels.single = self._create_neo_relationship(node, other)\n\n  File \"/opt/phaidra/env/src/neo4django/neo4django/db/models/relationships.py\", line 377,
in _create_neo_relationship\n    if obj.pk is None:
AttributeError: 'unicode' object has no attribute 'pk'\n"}

设置非关系属性可以正常工作。创建一个新的提交返回相同的错误,但创建是在没有“崩溃”关系的情况下完成的。我想知道这是否不适用于 neo4django 的 ModelRessource。非常感谢。

4

0 回答 0