我正在一个项目上,它是用于记录员工详细信息的“公司数据”。我有一个名为“ skill”的“ manytomany”字段,我想通过“ post request”向邮递员分配值。
def create_employee(cls, email, password, first_name, last_name, mobile_number, dob, nationality, skill):
user = User.objects.create(first_name=first_name, last_name=last_name, email=email, username=email)
if not password:
password = User.objects.make_random_password()
user.set_password(password)
user.save()
dob_obj = datetime.datetime.strptime(dob, "%Y-%m-%d").date()
emp = cls.objects.create(user=user, email=email, mobile_number=mobile_number, dob=dob_obj,
nationality=nationality, skill=skill.set())
return emp
views.py
类RegistrationView(APIView): Permission_classes =() authentication_classes =()
def post(self, request):
email = request.data.get('email', '')
password = request.data.get('password', '')
first_name = request.data.get('first_name', '')
last_name = request.data.get('last_name', '')
mobile_number = request.data.get('mobile_number', '')
dob = request.data.get('dob', '')
nationality = request.data.get('nationality', '')
skill = request.data.get('skill', '')
if not email:
return Response({"success": False, "message": "An error occurred"},status=status.HTTP_400_BAD_REQUEST)
try:
if User.objects.filter(email=email).exists():
return Response({"success": False, "message": "Email ID already registered. Please login"},
status=status.HTTP_400_BAD_REQUEST)
employee = Employee.create_employee(email, password, first_name, last_name,mobile_number, dob, nationality, skill)
except:
return Response({"success": False, "message": "An error occurred. Please try again"},
status=status.HTTP_400_BAD_REQUEST)
return Response({"success": True, "profile": employee.get_profile(),
"token": employee.get_token()
})
预期:员工注册时应设置技能值。 实际:没有添加。