【发布时间】:2019-07-11 19:48:51
【问题描述】:
我创建了一个如下视图。类似的我必须创建CreateAPIView。在以下EmployeeAddView 中,我采用了两个forms 并据此添加了数据。但我不知道如何在 REST API 中做到这一点。
@login_required
def EmployeeAddView(request):
# Tab Opts Checking
if request.user.userprofile.user_company.company_tab_opts:
return redirect('admin_employee')
if request.method == 'POST':
random_password = User.objects.make_random_password() # generate password here
ur_form = EmployeeRegisterForm(request.POST, pwd=random_password)
pr_form = EmployeeProfileForm(request.POST, request.FILES, user_company=request.user.userprofile.user_company.id)
user_role = ACLRoles.objects.get(id=4)
if ur_form.is_valid() and pr_form.is_valid():
new_user = ur_form.save(commit=False)
new_user.username = new_user.email
new_user.set_password(random_password)
new_user.save()
profile = pr_form.save(commit=False)
if profile.user_id is None:
profile.user_id = new_user.id
profile.user_role_id = user_role.id
profile.user_company_id = request.user.userprofile.user_company.id
profile.save()
for group in request.POST.getlist('user_groups'): # For many to many field
profile.user_groups.add(group)
email= ur_form.cleaned_data.get('email')
messages.success(request, 'Employee Added - %s!' % email)
return redirect('admin_employee')
else:
ur_form = EmployeeRegisterForm(use_required_attribute=False)
pr_form = EmployeeProfileForm(user_company=request.user.userprofile.user_company.id, use_required_attribute=False)
return render(request, 'employee/employee_form.html', {'ur_form': ur_form, 'pr_form': pr_form})
这是我的尝试,但我不知道该怎么做 API-views.py
class EmployeeAddAPIView(generics.CreateAPIView):
queryset = User.objects.all()
serializer_class = EmployeeRegisterSerializer
permission_classes = [UserIsAuthenticated]
def perform_create(self, serializer):
serializer.save(
userprofile__user_company = self.request.auth.application.company,
)
API-Serializer.py
class EmployeeProfileSerializer(serializers.ModelSerializer):
class Meta:
model = UserProfile
fields = [ 'user_employee_id',
'user_phone',
'user_payroll_id',
'user_hire_date',
'user_pay_rate',
'user_salaried',
'user_excempt',
'user_groups',
'user_state',
'user_city',
'user_zipcode',
'user_status',
]
class EmployeeRegisterSerializer(serializers.ModelSerializer):
userprofile = EmployeeProfileSerializer(read_only=True)
class Meta:
model = User
fields = ['first_name','last_name', 'email', 'userprofile']
我该怎么做。
我做了这样的事情
from rest_framework.views import APIView
from rest_framework import status
class EmployeeAddAPIView(APIView):
def post(self, request, *args, **kwrgs):
serializer1 = EmployeeRegisterSerializer(data=request.data)
serializer2 = EmployeeProfileSerializer(data=request.data)
user_role = ACLRoles.objects.get(id=4)
if serializer1.is_valid():
print('inside')
new_user = serializer1.save(commit=False)
new_user.username = new_user.email
new_user.set_password(random_password)
new_user.save()
profile = serializer2.save(commit=False)
if profile.user_id is None:
profile.user_id = new_user.id
profile.user_role_id = user_role.id
profile.user_company_id = self.request.auth.application.company
profile.save()
return Response(status=status.HTTP_200_OK)
print('outside', serializer1)
return Response(status=status.HTTP_200_OK)
【问题讨论】:
-
你能显示完整的错误日志吗
-
抱歉,我删除了该代码 - 我使用了其他一些未验证的代码。它没有进入
if serializer1.is_valid() and serializer2.is_valid(): -
尝试打印这样的错误 --> print(serializer1.errors)
-
AssertionError: 'commit' 不是 'save()' 方法的有效关键字参数。如果您需要在提交到数据库之前访问数据,请改为检查“serializer.validated_data”。如果您需要在保存的模型实例上设置额外的属性,您还可以将额外的关键字参数传递给“save()”。例如:'serializer.save(owner=request.user)'。'
-
drf 序列化程序不像 django 表单
标签: django django-models django-forms django-rest-framework django-views