1👍
@csrf_exempt
def companies_create(request):
user = get_user_from_somewhere()
if request.method == 'POST':
data = JSONParser().parse(request)
serializer = CompanySerializer(data=data)
print(serializer)
if serializer.is_valid():
serializer.save(user=user)
return JsonResponse(serializer.data, status=201)
return JsonResponse(serializer.errors, status=400)
1👍
use django get_user_model,
from django.contrib.auth import get_user_model
user_model = get_user_model()
@csrf_exempt
def companies_create(request):
user = get_user_from_somewhere()
if request.method == 'POST':
data = JSONParser().parse(request)
serializer = CompanySerializer(data=data)
print(serializer)
if serializer.is_valid():
serializer.save(user=user_model)
return JsonResponse(serializer.data, status=201)
return JsonResponse(serializer.errors, status=400)
Source:stackexchange.com