I'm not sure if this is a django/drf question or just a simple Python question with handling args/kwaargs..
I created a function that alters some fields on incoming JSON and I am trying to call it from a POST method in my views.py
The issue is I need the function to handle a variable number of variables as well as the django request object and I am not sure how to handle this without getting an error.
Here is my current code:
def post(self, request, person, city, format=None):
request = PreSerializer(request, person, city)
serializer = CreateRecordSerializer(data=request.data)
if serializer.is_valid():
def PreSerializer(request, *args):
if person = "david":
person = 1:
# Converts strings in JSON to foreignkey ID's
if 'name' in request.data:
person_record = get_object_or_404(Person, name=[request.data['person'])
request.data["person"] = person_record.id
if 'city' in request.data:
city_record = get_object_or_404(City, name=request.data['city])
request.data["city"] = city_record.id
return request
I believe the issue is related to (request, *args)
if person
UnboundLocalError: local variable 'person' referenced before assignment
You can either change the signature of your function to include person
def PreSerializer(request, person, *args):
if person = "david":
...
Or, if you can get person from args
.
def PreSerializer(request, *args):
person = args[0]
if person == "david":
...