在我的Django rest api中,我有一個視圖,通過創建一個新的債務模型實例並保存它來增加債務。如果此操作成功,則api應該返回一個包含新創建的債務模型實例的響應作爲json。在負責增加債務,並返回其模型中的功能是這樣的:如何在DRF發佈請求後將模型對象作爲json返回?
def add_debt(self, from_user, to_user,
paying_user, receiving_user,
amount, currency, description,
date_incurred, deadline, payed, overdue):
""" Create a debt request """
if from_user == to_user:
raise ValidationError("Users cannot be in debt with themselves")
request, created = DebtRequest.objects.get_or_create(
from_user=from_user,
to_user=to_user,
paying_user=paying_user,
receiving_user=receiving_user,
amount=amount,
currency=currency,
description=description,
date_incurred=date_incurred,
deadline=deadline,
payed=payed,
overdue=overdue)
debt_request_created.send(sender=request)
return request
在我看來,我有以下幾點:
class DebtRequestCreate(generics.GenericAPIView):
permission_classes = (IsAuthenticated,)
def post(self, request, *args, **kwargs):
other_user = User.objects.get(pk=request.data["friend_pk"])
amount = request.data["amount"]
currency = request.data["currency"]
description = request.data["description"]
date_incurred = request.data["date_incurred"]
deadline = request.data["deadline"]
is_payed = request.data["is_payed"]
is_overdue = request.data["is_overdue"]
is_in_debt = request.data["is_in_debt"]
debt = None
if is_in_debt: # is the request.user in debt
debt = Debt.objects.add_debt(
request.user, # From user
other_user, # To user
request.user, # Paying user
other_user, # Receiving user
amount, currency, description, date_incurred,
deadline, is_payed, is_overdue)
else:
debt = Debt.objects.add_debt(
request.user, # From user
other_user, # To user
other_user, # Paying user
request.user, # Receiving user
amount, currency, description, date_incurred,
deadline, is_payed, is_overdue)
return Response(status=201)
而不是返回的狀態代碼,我想返回一個JSON版本的debt
對象。我有以下的功能,我用它來Debt
對象列表轉換成JSON在另一種觀點認爲:
其他視圖重要代碼:
debt_requests_list = Debt.objects.requests(user=request.user)
debt_requests_list_json = json.loads(serializers.serialize('json', debt_requests_list))
debt_requests_json = [debt_requests_json_format(x, request.user.id)
for x in debt_requests_list_json]
return JsonResponse(debt_requests_json, safe=False)
功能:
def debt_requests_json_format(x, user_pk):
fields = x["fields"]
# if == True --> Debt request addressed to current user.
if fields["to_user"] == user_pk:
return {"pk": x["pk"],
"created": fields["created"],
"is_user_to_user": True,
"current_user_pk": user_pk,
"from_user": fields["from_user"],
"from_user_first_name": User.objects.get(pk=fields["from_user"]).firstName,
"from_user_last_name": User.objects.get(pk=fields["from_user"]).lastName,
"paying_user": fields["paying_user"],
"receiving_user": fields["receiving_user"],
"amount": fields["amount"],
"currency": fields["currency"],
"payed": fields["payed"],
"description": fields["description"],
"date_incurred": fields["date_incurred"],
"deadline": fields["deadline"],
"overdue": fields["overdue"]}
else: # if == False --> Debt request has been sent by current user.
return {"pk": x["pk"],
"created": fields["created"],
"is_user_to_user": False,
"current_user_pk": user_pk,
"to_user": fields["to_user"],
"to_user_first_name": User.objects.get(pk=fields["to_user"]).firstName,
"to_user_last_name": User.objects.get(pk=fields["to_user"]).lastName,
"paying_user": fields["paying_user"],
"receiving_user": fields["receiving_user"],
"amount": fields["amount"],
"currency": fields["currency"],
"payed": fields["payed"],
"description": fields["description"],
"date_incurred": fields["date_incurred"],
"deadline": fields["deadline"],
"overdue": fields["overdue"]}
我可以使用這個函數將單個債務對象轉換爲json並使用JsonResponse將其返回給我的客戶端?
爲什麼你不使用串行和createmodelmixin HTTP: //www.django-rest-framework.org/api-guide/generic-views/#createmodelmi xin? –
我想添加不在模型中的額外字段,所以我自己做。 –
但是,如何使用創建模型mixin –