'How to add extra key in created object in Django Rest Framework?
I'm new to Django
Currently I'm trying to add a simple extra key on my returned json in the serializer. Here's the create() override of my serializer:
def create(self, validated_data):
available_dates = validated_data.pop("available_dates")
# Default expiry time is 3 days
expiry = datetime.datetime.now() + datetime.timedelta(days=3)
event = Events.objects.create(**validated_data, expiry=expiry)
for selectedTime in available_dates:
AvailableDates.objects.create(
event=event,
**selectedTime,
)
signer = Signer()
signedObject = signer.sign_object(
{"expiry": expiry.isoformat(), "id": event.id}
)
return {**event, "signed_url": signedObject}
As you can see, I don't want to only return event, but I want to add a signed url.
This gave me an error:
'Events' object is not a mapping
So then how do I add extra key? What do I return in the create function? Can I create Response() as the return of create function in DRF?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
