{"id":16533565,"url":"https://github.com/af/djangbone","last_synced_at":"2025-07-03T02:36:43.612Z","repository":{"id":1929282,"uuid":"2857834","full_name":"af/djangbone","owner":"af","description":"Simple Django backends for Backbone.js apps.","archived":false,"fork":false,"pushed_at":"2013-06-10T04:49:09.000Z","size":123,"stargazers_count":151,"open_issues_count":0,"forks_count":19,"subscribers_count":10,"default_branch":"master","last_synced_at":"2024-10-18T07:03:25.057Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/af.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2011-11-26T21:33:53.000Z","updated_at":"2024-04-23T12:58:58.000Z","dependencies_parsed_at":"2022-09-09T05:21:26.929Z","dependency_job_id":null,"html_url":"https://github.com/af/djangbone","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/af%2Fdjangbone","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/af%2Fdjangbone/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/af%2Fdjangbone/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/af%2Fdjangbone/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/af","download_url":"https://codeload.github.com/af/djangbone/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":233699713,"owners_count":18716261,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-11T18:15:14.829Z","updated_at":"2025-01-13T05:38:15.780Z","avatar_url":"https://github.com/af.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"=========\nDjangbone\n=========\n\nDjangbone is a small django app that makes it easy to work with `Backbone.js\n\u003chttp://backbonejs.org/\u003e`_ frontends. More specifically, it allows you to\nquickly build a web API that works with the default Backbone.sync implementation.\n\nDjangbone provides one abstract class-based view (BackboneAPIView), which has a\nbunch of hooks for customization.\n\n\nExample Usage\n-------------\n\nAfter downloading/installing djangbone, all you need to do is:\n\n#. Subclass ``BackboneAPIView``, and set the ``base_queryset`` and\n   ``serialize_fields`` attributes.\n#. Wire up the view subclass in your urlconf.\n\nFor example, in myapp/views.py::\n\n    from myapp.models import Widget\n    from djangbone.views import BackboneAPIView\n\n    class WidgetView(BackboneAPIView):\n        # base_queryset is a queryset that contains all the objects that are\n        # accessible by the API:\n        base_queryset = Widget.objects.all()\n\n        # serialize_fields is a list of model fields that you want to be sent\n        # in your JSON resonses:\n        serialize_fields = ('id', 'name', 'description', 'created_at')\n\nIn myapp/urls.py::\n\n    from myapp.views import WidgetView\n\n    # Create url patterns for both \"collections\" and single items:\n    urlpatterns = patterns('',\n        url(r'^widgets$', WidgetView.as_view()),\n        url(r'^widgets/(?P\u003cid\u003e\\d+)', WidgetView.as_view()),\n    )\n\nIf you want to run the djangbone tests, you'll need to add ``\"djangbone\"`` to your\nINSTALLED_APPS, and run ``python manage.py test djangbone``. The tests use\n``django.contrib.auth``, so that app will also need to be in your INSTALLED_APPS\nfor the tests to work.\n\n\nHandling POST and PUT requests\n------------------------------\n\nBackbone.sync uses POST requests when new objects are created, and PUT requests\nwhen objects are changed. If you want to support these HTTP methods, you need to\nspecify which form classes to use for validation for each request type.\n\nTo do this, give your BackboneAPIView subclass ``add_form_class`` and\n``edit_form_class`` attributes. Usually you'll want to use a ModelForm\nfor both, but regardless, each form's save() method should return the model\ninstance that was created or modified.\n\nHere's an example (assume AddWidgetForm and EditWidgetForm are both ModelForms)::\n\n    from djangbone.views import BackboneAPIView\n    from myapp.models import Widget\n    from myapp.forms import AddWidgetForm, EditWidgetForm\n\n    class WidgetView(BackboneAPIView):\n        base_queryset = ...\n        serialize_fields = ...\n        add_form_class = AddWidgetForm      # Used for POST requests\n        edit_form_class = EditWidgetForm    # Used for PUT requests\n\nIf you need access to the ``request`` object in your form classes (maybe to\nsave ``request.user`` to your model, or perform extra validation), add\na ``set_request()`` method to your form classes as follows::\n\n    class AddWidgetForm(ModelForm):\n        class Meta:\n            model = Widget\n\n        def set_request(self, request):\n            self.request = request\n\n        # Now you have access to self.request in clean() and save()\n\n\nPagination\n----------\n\nIf you want to limit the number of items returned for a collection, you can\nturn on basic pagination with BackboneAPIView's ``page_size`` attribute. Set it to\nan integer and GETs without an ``id`` will be paginated. The default GET\nparameter is \"p\", but you can override this with\n``BackboneAPIView.page_param_name``.\n\n\nCustomization\n-------------\n\nThere's a decent chance that you'll want to wrap your BackboneAPIView subclass\nwith additional functionality, for example to only allow registered users to\naccess this view. You can use django's method_decorator on BackboneAPIView's\ndispatch() method to do this as follows::\n\n    from django.contrib.auth.decorators import login_required\n    from django.utils.decorators import method_decorator\n\n    class WidgetView(BackboneAPIView):\n        ...\n\n        @method_decorator(login_required)\n        def dispatch(self, request, *args, **kwargs):\n            return super(WidgetView, self).dispatch(*args, **kwargs)\n\n\nYou might also want to vary the base_queryset depending on the request (or an\nextra url parameter). You can also override dispatch() to do this, for example::\n\n    class WidgetView(BackboneAPIView):\n        base_queryset = Widgets.objects.all()\n\n        def dispatch(self, request, *args, **kwargs):\n            if request.method in ['PUT', 'DELETE']:\n                self.base_queryset = Widgets.objects.filter(owner=request.user)\n            return super(WidgetView, self).dispatch(*args, **kwargs)\n\n\nA Note on CSRF Protection\n-------------------------\n\nBackbone.sync sends POST request data as JSON, which doesn't work so well with\n`Django's built-in CSRF middleware \u003chttps://docs.djangoproject.com/en/1.3/ref/contrib/csrf/\u003e`_\n(the latter expects form-encoded POST data). As a result, if you're using the CSRF\nmiddleware, you'll want to either:\n\n#. Wrap your BackboneAPIView's dispatch method with the csrf_exempt decorator\n   to disable CSRF protection, or...\n#. (recommended) In javascript, configure jQuery's ajax method to always send\n   the ``X-CSRFToken`` HTTP header. See the `Django CSRF docs\n   \u003chttps://docs.djangoproject.com/en/1.3/ref/contrib/csrf/#ajax\u003e`_ for one way\n   to do it, or if you have ``{% csrf_token %}`` somewhere in your Django\n   template you can use something like::\n\n       // Setup $.ajax to always send an X-CSRFToken header:\n       var csrfToken = $('input[name=csrfmiddlewaretoken]').val();\n       $(document).ajaxSend(function(e, xhr, settings) {\n           xhr.setRequestHeader('X-CSRFToken', csrfToken);\n       });\n\n\nRequirements\n------------\n\nDjangbone uses class-based views, and as such will only work with Django 1.3\nand above. Python 2.6+ is also required.\n\nDjangbone makes a few assumptions about your models in order to work:\n\n    * Your model has an integer primary key named 'id' (Django creates this\n      field by default).\n    * The model fields in ``serialize_fields`` can be serialized to JSON.\n      This isn't a problem for simple CharFields, IntegerFields, etc, but\n      more complex fields will not work by default. You can fix this by\n      overriding ``BackboneAPIView.json_encoder`` with your own JSONEncoder subclass.\n      See the djangbone source for an example of this, which adds support for\n      serializing ``datetime`` instances.\n\n\nAlternatives\n------------\n\nDjangbone is designed to be a simple way to serialize your models to JSON in\na way that works with Backbone. It's not trying to be a generalized,\nformat-agnostic API generator. If that's what you're looking for, you probably\nwill want to go with something like django-tastypie or django-piston instead.\n\nIf you're already using django-tastypie, or are looking for a more full-featured API\nbackend than Djangbone provides, you may want to look at `backbone-tastypie\n\u003chttps://github.com/PaulUithol/backbone-tastypie\u003e`_, which overrides\nBackbone.sync (via javascript) in a way that works nicely with tastypie.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faf%2Fdjangbone","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faf%2Fdjangbone","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faf%2Fdjangbone/lists"}