gpt4 book ai didi

django - 如何使用 Tastypie 通过相关模型属性过滤 API 结果?

转载 作者:行者123 更新时间:2023-12-01 08:16:17 24 4
gpt4 key购买 nike

鉴于以下 API 定义,我需要能够按产品别名过滤事件。

鉴于事件属于工作而工作属于产品,我不确定如何指定。

api.py:

class ProductResource(ModelResource):

class Meta:
queryset = Product.objects.all()
resource_name = 'product'
allowed_methods = ['get']
excludes = ['created_at','updated_at']
filtering = {
'alias': ALL
}

class EnvironmentResource(ModelResource):

class Meta:
queryset = Environment.objects.all()
resource_name = 'environment'
allowed_methods = ['get']
excludes = ['created_at','updated_at']

class JobResource(ModelResource):

product = fields.ForeignKey(ProductResource, 'product')

class Meta:
queryset = Job.objects.all()
resource_name = 'job'
allowed_methods = ['get']
excludes = ['created_at','updated_at']

class EventResource(ModelResource):

environment = fields.ForeignKey(EnvironmentResource, 'environment',full=True)
job = fields.ForeignKey(JobResource, 'job',full=True)

class Meta:
queryset = Event.objects.all()
resource_name = 'event'
allowed_methods = ['get']
excludes = ['created_at','updated_at']
filtering = {
HOW DO I FILTER BY PRODUCT ALIAS????
}

最佳答案

鉴于以下过滤规范:

# In EventResource
filtering = {
'job' : ALL_WITH_RELATIONS
}

# In JobResource
filtering = {
'product' : ALL_WITH_RELATIONS
}

# In ProductResource
filtering = {
'alias' : ALL
}

你应该能够做到:
/api/events/job__product__alias=something

关于django - 如何使用 Tastypie 通过相关模型属性过滤 API 结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10147075/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com