[英]How to query on attribute of ToManyField with django-tastypie
Edit: This approach does work, I had a typo. 编辑:这种方法确实有效,我有一个错字。 Thanks @eran for pointing it out, fixed below. 感谢@eran指出来,已在下面进行了修复。
In console I can do this: 在控制台中,我可以这样做:
Performance.objects.filter(ticket_blocks__price__gt=200)
And get performances that have ticket blocks with price greater than $200. 并获得门票价格超过200美元的表演。 But this: 但是这个:
http://localhost:8000/api/v1/performance/?ticket_blocks__price__gt=200
Gives me KeyError: u'price'
. 给我KeyError: u'price'
。 What am I doing wrong? 我究竟做错了什么?
models.py models.py
class TicketBlock(models.Model):
name = models.CharField(max_length=100)
price = models.DecimalField(max_digits=8, decimal_places=2)
class Performance(models.Model):
start_time = models.DateTimeField(db_index=True)
end_time = models.DateTimeField(default=None, blank=True, null=True)
sold_out = models.BooleanField(default=False)
# All performance have at least one ticket block
ticket_blocks = models.ManyToManyField('TicketBlock')
api.py api.py
class TicketBlockResource(ModelResource):
class Meta:
queryset = TicketBlock.objects.all()
allowed_methods = ['get']
resource_name = 'ticket-block'
filtering = {
'price': ALL
}
class PerformanceResource(ModelResource):
ticket_blocks = fields.ToManyField(TicketBlockResource, 'ticket_blocks', blank=True, null=True, full=True)
class Meta:
queryset = Performance.objects.all()
allowed_methods = ['get']
resource_name = 'performance'
filtering = {
'ticket_blocks': ALL_WITH_RELATIONS
}
IT should work, I think you just have confusion in the definitions and used the wrong model VenueType
instead of TicketBlock
IT应该可以工作,我想您只是对定义感到困惑,并使用了错误的模型VenueType
而不是TicketBlock
Change the line: 更改行:
queryset = VenueType.objects.all()
to: 至:
TicketBlock.objects.all()
and I think it will fix your problem. 而且我认为它将解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.