Reputation: 6891
when I try to create a new instance of an Employee I get an error:
django.utils.datastructures.MultiValueDictKeyError: "'bio'"
When I print
this line print(request.data)
within the post method in 'EmployeeAddView` class
I get:
<QueryDict: {'joining_date': ['2018-03-04'], 'designation': ['1'], 'csrfmiddlewaretoken': ['5AeZ7lFOE2Z5j8cPNNZtygh208Esw65tvf5fzka56nCAj1oUFWCR3fcNHuOok2JK'], 'bio.marital_status': ['1'], 'bio.preferred_language': ['English'], 'tax_id_number': ['333333333ed'], 'bio.birthday': ['2018-03-04'], 'bio.user.first_name': ['Jack'], 'department': ['2'], 'bio.user.last_name': ['Sparrow'], 'bio.phone_number': ['9999999'], 'bio.main_id_type_no': ['459opppp'], 'bio.id_type': ['1'], 'bio.gender': ['1'], 'account_number': ['qwwwwwwww3r3']}>
Internal Server Error: /hr/employee_add/
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/django/utils/datastructures.py", line 83, in __getitem__
list_ = super(MultiValueDict, self).__getitem__(key)
KeyError: 'bio'
I have this at views.py
class EmployeeAddView(generics.CreateAPIView):
queryset=Employee.objects.all()
serializer_class=EmployeeSerializer
def post(self, request, format=None):
print(request.data)
designation = Group.objects.get(id=self.request.data['designation'],)
department = Group.objects.get(id=self.request.data['department'],)
bio = Bio.objects.get(id=self.request.data['bio'],)
employee = Employee.objects.create(
tax_id_number=request.data['tax_id_number'],
account_number=request.data['account_number'],
joining_date=request.data['joining_date'],
designation =designation,
department =department,
bio=bio,
)
return Response(status=status.HTTP_201_CREATED)
Then I have created a serializer like this:
# Nest Bio With User seriializer
class EmployeeSerializer(serializers.ModelSerializer):
# TODO: Define serializer fields here
bio = BioSerializer()
#user = UserSerializer()
class Meta:
model = Employee
# fields = ['user','tax_id_number','account_number','joining_date','designation','department','gender','marital_status','id_type','birthday','ethnicity','preferred_language','phone_number','em_contact','address']
fields = '__all__'
Upvotes: 2
Views: 3987
Reputation: 291
In your views.py file update your code as below
def post(self, request, format=None):
print(request.data)
designation = Group.objects.get(id=self.request.data.get('designation',None),)
department = Group.objects.get(id=self.request.data.get('department', None),)
bio = Bio.objects.get(id=self.request.data.get('bio', None),)
if designation and department and bio:
employee = Employee.objects.create(
tax_id_number=request.data['tax_id_number'],
account_number=request.data['account_number'],
joining_date=request.data['joining_date'],
designation =designation,
department =department,
bio=bio,
)
updating line as bio = Bio.objects.get(id=self.request.data.get('bio', None),) will give you none value if the key is missing or the key has None value, but It will not give you the MultiValueDictKeyError.
Upvotes: 3