ApathyBear
ApathyBear

Reputation: 9615

Django object filtering with variables

I have model that looks like this:

class Beacon(models.Model):
    name = models.CharField()
    bid =  models.CharField()
    campaigns = models.ManyToMany(Campaign)
    location = models.ForeignKey(Location)

In my api view, I am trying to find a specific Beacon given a bid (this is beacon id btw). So I have something that looks like this:

def SawBeacon(request, beacon_id):

    if request.method == 'GET':
        Beacon  = Beacon.objects.filter(bid__beacon_id=%s) % beacon_id

This doesn't work.. But I think you get the idea of what I am trying to do. I want to take the incoming beacon_id argument and filter down to a specific beacon that matches this ID.

  1. How would I do this?
  2. When I filter down an object, do I get the object itself or will I get the value?

Upvotes: 1

Views: 73

Answers (1)

Alex
Alex

Reputation: 8539

If you're getting one instance, you'll want to use get rather than filter. Try:

Beacon.objects.get(bid=beacon_id)

When you use filter, you'll return a queryset.

Upvotes: 2

Related Questions