0

我正在尝试将评论模型添加到我网站上的另一个模型。这是资源:

class CommentResource(ModelResource):

    user = fields.ForeignKey(UserResource, 'user', readonly=True)
    question = fields.ForeignKey(QuestionResource, 'question', readonly=True)

    def hydrate_user(self, bundle):
        return bundle.request.user

    class Meta:
        queryset = Comment.objects.all().order_by("-created")
        resource_name = 'comment'
        filtering = {
            "session": ALL_WITH_RELATIONS,
            "user": ALL_WITH_RELATIONS
        }
        authorization = CommentAuthorization()

模型:

class Comment(models.Model):

    question = models.ForeignKey(AMAQuestion, related_name = "comments")
    user = models.ForeignKey(User, related_name = "comments")

    created = models.DateTimeField(auto_now_add=True, editable=False)
    edited = models.DateTimeField(auto_now=True, editable=False)

    comment = models.TextField()

当尝试对此进行发布时,似乎美味派没有正确设置问题。(由于某种原因没有调用 hydrate_FOO 方法也可能很重要。

这是我发送的一些示例数据:

{
    "comment": "test",
    "question": "/api/v1/question/1/"
}

这是错误:

Traceback (most recent call last):

  File "C:\Python33\lib\site-packages\django\db\backends\sqlite3\base.py", line
362, in execute
    return Database.Cursor.execute(self, query, params)

sqlite3.IntegrityError: questions_comment.question_id may not be NULL


During handling of the above exception, another exception occurred:


Traceback (most recent call last):

  File "C:\Python33\lib\site-packages\tastypie\resources.py", line 195, in wrapp
er
    response = callback(request, *args, **kwargs)

  File "C:\Python33\lib\site-packages\tastypie\resources.py", line 426, in dispa
tch_list
    return self.dispatch('list', request, **kwargs)

  File "C:\Python33\lib\site-packages\tastypie\resources.py", line 458, in dispa
tch
    response = method(request, **kwargs)

  File "C:\Python33\lib\site-packages\tastypie\resources.py", line 1320, in post
_list
    updated_bundle = self.obj_create(bundle, **self.remove_api_resource_names(kw
args))

  File "C:\Python33\lib\site-packages\tastypie\resources.py", line 2084, in obj_
create
    return self.save(bundle)

  File "C:\Python33\lib\site-packages\tastypie\resources.py", line 2230, in save

    bundle.obj.save()

  File "C:\Python33\lib\site-packages\django\db\models\base.py", line 546, in sa
ve
    force_update=force_update, update_fields=update_fields)

  File "C:\Python33\lib\site-packages\django\db\models\base.py", line 650, in sa
ve_base
    result = manager._insert([self], fields=fields, return_id=update_pk, using=u
sing, raw=raw)

  File "C:\Python33\lib\site-packages\django\db\models\manager.py", line 215, in
 _insert
    return insert_query(self.model, objs, fields, **kwargs)

  File "C:\Python33\lib\site-packages\django\db\models\query.py", line 1675, in
insert_query
    return query.get_compiler(using=using).execute_sql(return_id)

  File "C:\Python33\lib\site-packages\django\db\models\sql\compiler.py", line 93
7, in execute_sql
    cursor.execute(sql, params)

  File "C:\Python33\lib\site-packages\django\db\backends\util.py", line 41, in e
xecute
    return self.cursor.execute(sql, params)

  File "C:\Python33\lib\site-packages\django\db\backends\sqlite3\base.py", line
364, in execute
    six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.
exc_info()[2])

  File "C:\Python33\lib\site-packages\django\utils\six.py", line 328, in reraise

    raise value.with_traceback(tb)

  File "C:\Python33\lib\site-packages\django\db\backends\sqlite3\base.py", line
362, in execute
    return Database.Cursor.execute(self, query, params)

django.db.utils.IntegrityError: questions_comment.question_id may not be NULL
4

2 回答 2

1

正如您所指出的,readonly是管理水合物循环中属性的属性。它实际上与阻止该字段在下一个请求中可编辑没有任何关系。如果我理解正确,您希望防止question将来编辑该字段。您可以通过停用对资源的修改或控制这些修改来做到这一点。

如果您想防止资源被修改,只需PUT通过将下一行添加到您的以下来停用对 Comment 资源的调用Meta

allowed_methods = ['get', 'post',]

另一方面,如果您想控制PUT行为方式,您可以控制水合物并从请求中删除新的“问题”,这将停止其修改:

def hydrate(self, bundle):
    if bundle.request.method == 'PUT':
        if 'question' in bundle.data:
            bundle.data.pop('question',None)
    return bundle
于 2013-08-29T14:53:25.897 回答
0

事实证明,tastepie 甚至没有在第一个 POST 上设置只读字段。我想这是有道理的,但我仍然想要只能在初始 POST 时编辑的字段。

于 2013-08-29T05:35:33.957 回答