我有一个Tastypie资源(TaskImagesResource),它需要覆盖obj_get_list函数,以便在向API发出请求时返回正确的数据。我需要访问另一个资源的属性(TaskResource,它与TaskImagesResource具有OneToOne关系)才能执行此操作。我可以使用get_related_resource()获取Task资源的实例,但我不知道如何从对象中获取数据。这是我现在的代码:
class TaskResource(ModelResource):
target = fields.OneToOneField(TargetResource, 'target', full=True)
activity = fields.ForeignKey(ActivityResource, 'activity')
time = fields.OneToOneField(TaskEventTimeResource, 'time', full=True)
class Meta:
queryset = Task.objects.all()
filtering = {
'id' : ALL,
'activity' : ALL_WITH_RELATIONS,
}
class TaskImagesResource(Resource):
task = fields.ForeignKey(TaskResource, 'task', full=True)
camera = fields.CharField()
def obj_get_list(self, request, **kwargs):
task_resource = self.task.get_related_resource(self)
task = Task.objects.get(id=task_resource.id)
image_list = task.sorted_imageinstances(self.camera)
但是我收到错误“int()参数必须是字符串或数字,而不是'CharField'”因为task_resource.id是CharField对象而不是数据本身。如何访问实际的现场数据?
答案 0 :(得分:0)
尝试:
task = Task.objects.get(id=int(task_resource.id))