Question

Edit: This approach does work, I had a typo. Thanks @eran for pointing it out, fixed below.

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. But this:

http://localhost:8000/api/v1/performance/?ticket_blocks__price__gt=200

Gives me KeyError: u'price'. What am I doing wrong?

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

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
        }
Was it helpful?

Solution

IT should work, I think you just have confusion in the definitions and used the wrong model VenueType instead of TicketBlock

Change the line:

queryset = VenueType.objects.all()

to:

TicketBlock.objects.all()

and I think it will fix your problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top