An easy to set up Django Rest Framework API that is compatible with the USDA Nutrient Database.
- Python (2.6.5+, 2.7, 3.2, 3.3, 3.4)
- Django (1.4.11+, 1.5.5+, 1.6, 1.7)
- Django Rest Framework (2.4.4)
To quickly see what this package can do, go to the Demo repository.
- You can install Django USDA with Python PIP:
pip install git+https://github.com/zundrium/django-usda.git
- Now you can add Django USDA to your
INSTALLED_APPS
in thesettings.py
of your project together with the required dependencies.
INSTALLED_APPS = (
...
'rest_framework',
'django_usda',
)
- Add the following settings to the
settings.py
for Django Rest Framework to function properly.
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
],
'DEFAULT_FILTER_BACKENDS': ('rest_framework.filters.DjangoFilterBackend',),
'PAGINATE_BY': 250,
'PAGINATE_BY_PARAM': 'page_size',
'MAX_PAGINATE_BY': 250
}
- After that add the ViewSets that you want to use and the required url patterns to the
urls.py
of your project.
from django_usda.modelviewsets import FoodViewSet, FoodGroupViewSet, FoodLanguaLFactorViewSet, LanguaLFactorViewSet, NutrientDataViewSet, NutrientViewSet, SourceViewSet, DerivationViewSet, WeightViewSet, FootnoteViewSet, DataLinkViewSet, DataSourceViewSet, FoodInfoViewSet
from django.contrib import admin
router = routers.DefaultRouter()
router.register(r'foods', FoodViewSet)
router.register(r'foodgroups', FoodGroupViewSet)
router.register(r'foodlangualfactors', FoodLanguaLFactorViewSet)
router.register(r'langualfactors', LanguaLFactorViewSet)
router.register(r'nutrientdatas', NutrientDataViewSet)
router.register(r'nutrients', NutrientViewSet)
router.register(r'sources', SourceViewSet)
router.register(r'derivations', DerivationViewSet)
router.register(r'weights', WeightViewSet)
router.register(r'footnotes', FootnoteViewSet)
router.register(r'datalinks', DataLinkViewSet)
router.register(r'datasources', DataSourceViewSet)
router.register(r'foodinfo', FoodInfoViewSet)
urlpatterns = patterns('',
...
url(r'^', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
)
-
Run
python manage.py migrate
if you have South or Django 1.7 installed. Otherwise usepython manage.py syncdb
. -
Download the ASCII version of the 27th release of the USDA Nutrient Database.
-
Run
python manage.py import_r27 <path_to_zipfile>
. This can take up to 10 minutes. -
Start the development server (Normally
python manage.py runserver
). -
That's it, now you can use the viewsets in your application! (Example:
http://localhost:8000/foodinfo/01001
).