我试图通过以下方法获取最后插入的数据
latest = AudioContentModel.objects.latest('id').id
object = model_to_dict(AudioContentModel.objects.get(pk=latest))
但出现以下错误
TypeError: Object of type 'FieldFile' is not JSON serializable
如何从上次插入的表中获取数据?
这是我的模特。py
class AudioContentModel(models.Model):
background_music = models.FileField(upload_to='documents/')
tts = models.FileField(upload_to='documents/')
final_audio = models.FileField(upload_to='documents/')
created = models.DateField(auto_now_add=True)
这是我的观点。py
def index(request):
if request.method == 'POST':
if request.is_ajax():
audiouploadform = AudioUploadForm(request.POST, request.FILES)
if audiouploadform.is_valid():
audiouploadform.save()
latest = AudioContentModel.objects.latest('id').id
print('Need to get the all the data from the latest and resturn as a JsonResponse')
data{
'background_music':
'tts':
'id':
}
return JsonResponse({'error': False, 'data': data})
else:
return JsonResponse({'error': True, 'errors': audiouploadform.errors})
else:
error = {
'message': 'Error! Must be an Ajax call'
}
return JsonResponse(error, content_type="application/json")
else:
audiouploadform = AudioUploadForm()
all_audio_files = AudioContentModel.objects.all()
data = {
'audio_file_list': all_audio_files,
'audiouploadform': audiouploadform,
}
return render(request, template_name='index.html', context=data)
答案 0 :(得分:0)
我知道了。
latest = AudioContentModel.objects.latest('id').id
print(latest)
instance = AudioContentModel.objects.get(pk=latest)
data = {
'id': instance.id,
'background_music': instance.background_music.url,
'tts': instance.tts.url,
'created':instance.created,
}