[Fixed]-How to return id after saving a record django rest frwamework

31👍

You need to assign the value being returned from the save() method:

obj = empresa.save()
print(obj.id) 

The reason is that save() calls create() of the Serializer which returns an object instance

4👍

Hi you need do like this

empresa = EmpresaCreateSerializer(data=data)
empresa.user = user_id
if(empresa.is_valid()):
    empresa.save() //Here you need use these two methods **create,perform_create**
    print(empresa.data['id'])
👤Robert

Leave a comment