Django ease of building a RESTful interface

前端 未结 6 907
渐次进展
渐次进展 2020-12-25 09:43

I\'m looking for an excuse to learn Django for a new project that has come up. Typically I like to build RESTful server-side interfaces where a URL maps to resources that sp

相关标签:
6条回答
  • 2020-12-25 10:04

    (I had to edit out the most obvious links.)

    +1 for piston - (link above). I had used apibuilder (Washington Times open source) in the past, but Piston works easier for me. The most difficult thing for me is in figuring out my URL structures for the API, and to help with the regular expressions. I've also used surlex which makes that chore much easier.

    Example, using this model for Group (from a timetable system we're working on):

    class Group(models.Model):
        """
        Tree-like structure that holds groups that may have other groups as leaves. 
        For example ``st01gp01`` is part of ``stage1``.
        This allows subgroups to work. The name is ``parents``, i.e.::
    
            >>> stage1group01 = Group.objects.get(unique_name = 'St 1 Gp01')
            >>> stage1group01
            >>> <Group: St 1 Gp01>
            # get the parents...
            >>> stage1group01.parents.all()
            >>> [<Group: Stage 1>]
    
        ``symmetrical`` on ``subgroup`` is needed to allow the 'parents' attribute to be 'visible'.
        """
        subgroup = models.ManyToManyField("Group", related_name = "parents", symmetrical= False, blank=True)
        unique_name = models.CharField(max_length=255)
        name = models.CharField(max_length=255)
        academic_year = models.CharField(max_length=255)
        dept_id = models.CharField(max_length=255)
        class Meta:
            db_table = u'timetable_group'
        def __unicode__(self):
            return "%s" % self.name
    

    And this urls.py fragment (note that surlex allows regular expression macros to be set up easily):

    from surlex.dj import surl
    from surlex import register_macro
    from piston.resource import Resource
    from api.handlers import GroupHandler
    group_handler = Resource(GroupHandler)
    
    # add another macro to our 'surl' function
    # this picks up our module definitions
    register_macro('t', r'[\w\W ,-]+')
    
    urlpatterns = patterns('',
    # group handler
    # all groups
    url(r'^groups/$', group_handler),
    surl(r'^group/<id:#>/$', group_handler),
    surl(r'^group/<name:t>/$', group_handler),)
    

    Then this handler will look after JSON output (by default) and can also do XML and YAML.

    class GroupHandler(BaseHandler):
        """
        Entry point for Group model
        """
    
        allowed_methods = ('GET', )
        model = Group
        fields = ('id', 'unique_name', 'name', 'dept_id', 'academic_year', 'subgroup')
    
        def read(self, request, id=None, name=None):
            base = Group.objects
            if id:
                print self.__class__, 'ID'
                try:
                    return base.get(id=id)
                except ObjectDoesNotExist:
                    return rc.NOT_FOUND
                except MultipleObjectsReturned: # Should never happen, since we're using a primary key.
                    return rc.BAD_REQUEST
            else:
                if name:
                    print self.__class__, 'Name'
                    return base.filter(unique_name = name).all()
                else:
                    print self.__class__, 'NO ID'
                    return base.all()
    

    As you can see, most of the handler code is in figuring out what parameters are being passed in urlpatterns.

    Some example URLs are api/groups/, api/group/3301/ and api/group/st1gp01/ - all of which will output JSON.

    0 讨论(0)
  • 2020-12-25 10:09

    Take a look at Piston, it's a mini-framework for Django for creating RESTful APIs.

    A recent blog post by Eric Holscher provides some more insight on the PROs of using Piston: Large Problems in Django, Mostly Solved: APIs

    0 讨论(0)
  • 2020-12-25 10:11

    Regarding your comment about not liking 3rd party code - that's too bad because the pluggable apps are one of django's greatest features. Like others answered, piston will do most of the work for you.

    0 讨论(0)
  • 2020-12-25 10:15

    This is probably pretty easy to do.

    URL mappings are easy to construct, for example:

    urlpatterns = patterns('books.views',
      (r'^books/$', 'index'),
      (r'^books/(\d+)/$', 'get'))
    

    Django supports model serialization, so it's easy to turn models into XML:

    from django.core import serializers
    from models import Book
    
    data = serializers.serialize("xml", Book.objects.all())
    

    Combine the two with decorators and you can build fast, quick handlers:

    from django.http import HttpResponse
    from django.shortcuts import get_object_or_404
    
    def xml_view(func):
      def wrapper(*args, **kwargs):
        result = func(*args, **kwargs)
        return HttpResponse(serializers.serialize("xml", result),
            mimetype="text/xml")
      return wrapper
    
    @xml_view
    def index(request):
      return Books.objects.all()
    
    @xml_view
    def get(request, id):
      return get_object_or_404(Book, pk=id)
    
    0 讨论(0)
  • 2020-12-25 10:20

    A little over a year ago, I wrote a REST web service in Django for a large Seattle company that does streaming media on the Internet.

    Django was excellent for the purpose. As "a paid nerd" observed, the Django URL config is wonderful: you can set up your URLs just the way you want them, and have it serve up the appropriate objects.

    The one thing I didn't like: the Django ORM has absolutely no support for binary BLOBs. If you want to serve up photos or something, you will need to keep them in a file system, and not in a database. Because we were using multiple servers, I had to choose between writing my own BLOB support or finding some replication framework that would keep all the servers up to date with the latest binary data. (I chose to write my own BLOB support. It wasn't very hard, so I was actually annoyed that the Django guys didn't do that work. There should be one, and preferably only one, obvious way to do something.)

    I really like the Django ORM. It makes the database part really easy; you don't need to know any SQL. (I don't like SQL and I do like Python, so it's a double win.) The "admin interface", which you get for free, gives you a great way to look through your data, and to poke data in during testing and development.

    I recommend Django without reservation.

    0 讨论(0)
  • 2020-12-25 10:23

    It can respond with any kind of data. JSON/XML/PDF/pictures/CSV...

    Django itself comes with a set of serializers.

    Edit

    I just had a look at at Piston — looks promising. Best feature:

    Stays out of your way.

    :)

    0 讨论(0)
提交回复
热议问题