Friday, February 24, 2012 Heres the plan. Friday, February 24, 2012 1. Model resourcefully. Friday, February 24, 2012 2. URLs are queries. Friday, February 24, 2012 3. Non-modeled data. Friday, February 24, 2012 4. When things go awry. Friday, February 24, 2012 Lets get this party started. Friday, February 24, 2012 1. Model resourcefully. Friday, February 24, 2012 models.py class Team(models.Model): name = models.CharField(max_length=255) city = models.CharField(max_length=255) . . . class Player(models.Model): team = models.ForeignKey(Team) rst_name = models.CharField(max_length=255) last_name = models.CharField(max_length=255) . . . class Game(models.Model): teams = models.ManyToManyField(Team) date_and_time = models.DateTimeField() . . . Friday, February 24, 2012 OMFG CODE LOL Friday, February 24, 2012 models.py class Team(models.Model): name = models.CharField(max_length=255) city = models.CharField(max_length=255) . . . class Player(models.Model): team = models.ForeignKey(Team) rst_name = models.CharField(max_length=255) last_name = models.CharField(max_length=255) . . . class Game(models.Model): teams = models.ManyToManyField(Team) date_and_time = models.DateTimeField() . . . Friday, February 24, 2012 api.py from tastypie.resources import ModelResource from tastypie import elds from tastypie.api import Api from tastypie.constants import ALL from tastypie.constants import ALL_WITH_RELATIONS Friday, February 24, 2012 api.py . . . class TeamResource(ModelResource): class Meta: queryset = Team.objects.all() resource_name = teams allowed_methods = [ get ] ltering = { name : ALL, city : ALL, } ordering = ltering
. . . Friday, February 24, 2012 api.py . . . class PlayerResource(ModelResource): team = elds.ForeignKey(my_app.TeamResource, team) class Meta: queryset = Player.objects.all() resource_name = players allowed_methods = [ get ] ltering = { rst_name : ALL, last_name : ALL, team : ALL_WITH_RELATIONS } ordering = ltering
. . . Friday, February 24, 2012 /api/v1/players/ /api/v1/teams/ Friday, February 24, 2012 MEME Friday, February 24, 2012 Friday, February 24, 2012 2. URLs are queries. Friday, February 24, 2012 api.py . . . class PlayerResource(ModelResource): team = elds.ForeignKey(my_app.TeamResource, team) class Meta: queryset = Player.objects.all() resource_name = players allowed_methods = [ get ] ltering = { rst_name : ALL, last_name : ALL, team : ALL_WITH_RELATIONS } ordering = ltering
. . . Friday, February 24, 2012 All players on a team? Friday, February 24, 2012 /api/v1/players/?team__name=Redskins Friday, February 24, 2012 api.py . . . class PlayerResource(ModelResource): team = elds.ForeignKey(my_app.TeamResource, team) class Meta: queryset = Player.objects.all() resource_name = players allowed_methods = [ get ] ltering = { rst_name : ALL, last_name : ALL, team : ALL_WITH_RELATIONS } ordering = ltering
. . . Friday, February 24, 2012 Order alphabetically? Friday, February 24, 2012 /api/v1/players/?order_by=last_name Friday, February 24, 2012 Friday, February 24, 2012 3. Non-modeled data. Friday, February 24, 2012 api.py . . . class GameResource(ModelResource): teams = elds.ManyToMany(my_app.TeamResource, team) class Meta: queryset = Player.objects.all() resource_name = players allowed_methods = [ get ] ltering = { game_date_time : ALL, teams : ALL_WITH_RELATIONS } ordering = ltering
. . . Friday, February 24, 2012 api.py . . . class GameResource(ModelResource): teams = elds.ManyToMany(my_app.TeamResource, team) day_of_the_week = elds.CharField() class Meta: queryset = Player.objects.all() resource_name = players allowed_methods = [ get ] ltering = { game_date_time : ALL, teams : ALL_WITH_RELATIONS } ordering = ltering
. . . Friday, February 24, 2012 api.py . . . class GameResource(ModelResource): teams = elds.ManyToMany(my_app.TeamResource, team) day_of_the_week = elds.CharField() . . . def dehydrate_day_of_the_week(self, bundle): return bundle.obj.game_date_time.strftime(%a) Friday, February 24, 2012 Friday, February 24, 2012 4. When things go awry. Friday, February 24, 2012 Or, when you code like @jasonbartz. Friday, February 24, 2012 Avoid /foo/?limit=0 Friday, February 24, 2012 Avoid /foo/?non_indexed_eld__icontains=foo Friday, February 24, 2012 Avoid /foo/?callback=a_long_ever_changing_number Friday, February 24, 2012 May the power of Bradlee compel you. Friday, February 24, 2012