Writing a backend for a mobile application in a few minutes

    Hello! My main area of ​​activity is the development of mobile applications (iOS, Android). And most of the applications use interaction with other users, data storage and other tasks that require a single server. Therefore, for most applications, you have to write your bike backend. And since I am mainly a mobile developer, writing this service always becomes a small problem - you have to use a web developer or look for a suitable BaaS service, even if you need to write just a couple of requests.
    Therefore, it was decided to try to find a tool that allows you to quickly write a small web service that could be used in a mobile application.

    The initial data was taken: knowledge of what HTTP, REST, JSON and the initial level of development in Python (Django).
    And the other day, a small project caught my eye, on which it was possible to conduct field trials. The essence of the project is an application for one event. It is necessary to display speakers and their reports.
    After a short search, the following tools were taken as a basis: Python (as the main development language), Django (as the base platform), and the Tastypie framework (presented as the framework for creating web services APIs). So let's get started.
    Create a Django application template:
    python django-admin.py startproject EventApp
    

    In the settings file settings.py we prescribe the necessary settings for the database, localization, time. Install the tastypie package:
    pip install django-tastypie
    

    Please note that Python2.6 + and Django 1.5+ are required . Due to ignorance of this fact, I had to spend a little more time, because the framework refused to work. In addition, you need to install the python-mimeparse package in the same way.
    Next, in the settings.py file, write:
    INSTALLED_APPS += ['tastypie']
    

    or add the 'tastypie' application to the existing list.
    Now let's write the models of our subject area:
    # -*- coding: utf-8 -*-
    from django.db import models
    class Speaker(models.Model):
        name = models.CharField(max_length=32)
        company = models.CharField(max_length=32)
        photo = models.ImageField(upload_to='photos', blank=True, null=True)
        def __unicode__(self):
            return self.name + ' (' + self.company + ')'
    class Event(models.Model):
        title = models.CharField(max_length=64)
        speaker = models.ForeignKey(Speaker, blank=False, null=False)
        start_time = models.TimeField()
        end_time = models.TimeField()
        def __unicode__(self):
            return self.title + ' (' + self.speaker.name + ')'
    

    We wrote a Speaker model and an Event model. Each performance must have a speaker. Now, we will make it so that we can fully work with our models as resources through the REST protocol.
    We create the api package in our application and the resources.py file (or you can create it in the main package).
    from tastypie.resources import ModelResource
    from EventApp.models import Speaker, Event
    class SpeakerResource(ModelResource):
        class Meta:
            queryset = Speaker.objects.all()
            resource_name = 'speaker'
    class EventResource(ModelResource):
        speaker = fields.ForeignKey(SpeakerResources, 'speaker', blank=True, null=True)
        class Meta:
            queryset = Event.objects.all()
            resource_name = 'event'
    

    In this file we created classes of so-called resources, the main objects in our REST service. These are just the resources that we will turn to. Each class contains a link to the model that it represents. The queryset field returns us a set of objects received from the database when accessing this resource. The resource_name field is optional, and allows us to specify in addition the name of the resource by which it will be available to us.
    Another point, in the EventResources class, we specified a separate speaker field, which indicates that the event resource refers to the speaker resource.
    Now it remains only to register in the urls.py file calls to our service. It makes it very simple.
    from django.conf.urls.defaults import *
    from tastypie.api import Api
    from api.resources import EventResource, SpeakerResource
    v1_api = Api(api_name='v1')
    v1_api.register(SpeakerResource())
    v1_api.register(EventResource())
    urlpatterns = patterns('',
        (r'^api/', include(v1_api.urls)),
    )
    

    Now we launch our project
    python manage.py runserver
    

    Now, if the server has successfully started by opening the page in the browser at the address http: // localhost: 8000 / api / entry /? Format = json , we will see there that the framework sees all our resources and displays the diagram of our service:
    {
      "events": 
      {
        "list_endpoint": "/api/v1/events/"
        , "schema": "/api/v1/events/schema/"
      }
      ,"speakers": 
      {
        "list_endpoint": "/api/v1/speakers/"
        ,"schema": "/api/v1/speakers/schema/"
      }
    }
    

    The format parameter forcibly indicates in which format we want to receive the data, but instead of it, you can specify the Content-type: application / json header in the request. In addition to JSON, xml, yaml, bplist are supported.
    At the schema address, you can see a description of the model structure (fields, types and description), and at list_endpoint you can already get our resources, which we previously recorded in the database.
    Now, opening the address http: // localhost: 8000 / api / v1 / events /? Format = json we will see there something like this:
    {
      "meta": 
      {
        "limit": 20
        ,"next": null
        ,"offset": 0
        ,"previous": null
        ,"total_count": 4
      }
      ,"objects": [
      {
        "id": 3
        ,"speaker": "/api/v1/speakers/2/"
        ,"start_time": "08:39:25"
        ,"end_time": "18:39:29"
        ,"title": "Ранее что нибудь"
        ,"description": "описание"
      }
      ]
    }
    

    As you can see - nothing complicated. In the meta section, basic information about the resource is displayed: the number of records, the size of the output, etc. At the same time, we can refer to a specific event by contacting the resource by its id - http: // localhost: 8000 / api / v1 / events / 1 /
    We can create a record by executing a POST request and passing an object into it in JSON format, update the PUT record with a request and delete using DELETE.
    Also, in tastypie, the ModelResource class has a large set of redefinable fields and methods with which we can completely change the structure of the output data. For example, instead of linking to a speaker, we want to immediately get his name so as not to make an extra request. In the EventResource class, we override the dehydrate method:
        def dehydrate(self, bundle):
            try:
                speaker = Speaker.objects.filter(id=bundle.obj.speaker.id)
                bundle.data['speaker_name'] = speaker[0].name
            except Speaker.DoesNotExist:
                pass
            return bundle
    

    In it, we find the speaker in the database and substitute it into the bundle object, which is a dictionary that is given by the resource. Now, the response to the request will look like this (I will write only the main part):
    {
      "id": 3
      ,"speaker": "/api/v1/speakers/2/"
      ,"speaker_name": "Василий"
      ,"start_time": "08:39:25"
      ,"end_time": "18:39:29"
      ,"title": "Ранее что нибудь"
      ,"description": "описание"
    }
    

    What we needed! In addition, a request to a resource can be made with parameters for filtering.
    For example, we need to select all events for one speaker. The following request would be logical:
    http: // localhost: 8000 / api / v1 / events /? Speaker = 1 which will return to us events whose speakers are the speaker with id = 1. You only need to register one more field in the meta resource class:
            filtering = {
                'speaker': ALL_WITH_RELATIONS
            }
    


    Conclusion

    Excellent! Now we can already access the server, receive data from it. But this is exactly what we needed.
    If the article is of interest, then you can continue the story about how to add validation, authorization here and tie a nice admin panel in such a short time.

    Also popular now: