本文整理匯總了Python中comments.models.Comment.add_root方法的典型用法代碼示例。如果您正苦於以下問題:Python Comment.add_root方法的具體用法?Python Comment.add_root怎麽用?Python Comment.add_root使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類comments.models.Comment
的用法示例。
在下文中一共展示了Comment.add_root方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: post
# 需要導入模塊: from comments.models import Comment [as 別名]
# 或者: from comments.models.Comment import add_root [as 別名]
def post(self, request, *args, **kwargs):
if not request.user.is_authenticated():
return Response(status.HTTP_401_UNAUTHORIZED)
ct = ContentType.objects.get_for_model(MODELS_MAPPINGS[kwargs['model']])
try:
obj = ct.get_object_for_this_type(pk=kwargs['object_id'])
except ObjectDoesNotExist:
raise ErrorResponse(status.HTTP_404_NOT_FOUND)
form = CommentForm(request.POST)
if not form.is_valid():
raise ErrorResponse(status.HTTP_400_BAD_REQUEST)
data = {
'content_type': ct,
'object_id': kwargs['object_id'],
'user': request.user,
'created': datetime.datetime.now(),
'content': form.cleaned_data['content'],
}
parent = form.cleaned_data['parent']
if parent:
instance = parent.add_child(**data)
else:
instance = Comment.add_root(**data)
if request.is_ajax():
return Response(status.HTTP_201_CREATED)
return HttpResponseRedirect(obj.get_absolute_url())