Skip to content

Commit 3ef91fa

Browse files
committed
python 3, django 1.9 refactor
1 parent 5adc826 commit 3ef91fa

2 files changed

Lines changed: 60 additions & 30 deletions

File tree

2013/2013-07-29-create-a-super-basic-rest-api-with-django-tastypie.markdown

Lines changed: 59 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
1-
# Create a Super Basic REST API with django-tastypie
1+
# Create a Super Basic REST API with Django Tastypie
22

3-
One of my clients literally called thirty minutes ago (last Friday) needing a JSON payload based on a GET response from the data model. I installed [django-tastypie](http://tastypieapi.org/) and thirty minutes later had the project completed. Although this example is overly simplified, it's not far off from my real-world implementation.
3+
Let's set up a RESTful API with [Django Tastypie](http://tastypieapi.org/).
44

5-
> **Note:** This tutorial is using Python 3.5 and the latest Django 1.9.7.
5+
*Updates:*
66

7-
## Setup
7+
- 07/10/2016: Upgraded to the latest versions of Python (v[3.5.1](https://www.python.org/downloads/release/python-351/)), Django (v[1.9.7](https://docs.djangoproject.com/en/1.9/releases/1.9.7/)), and django-tastypie (v[13.3](https://github.com/django-tastypie/django-tastypie/releases/tag/v0.13.3)).
8+
9+
<br>
10+
11+
<div class="center-text">
12+
<img class="no-border" src="/images/blog_images/django-tastypie/django-tastypie-logo.png" style="max-width: 100%;" alt="Django Tastypie">
13+
</div>
14+
15+
<br>
16+
17+
## Project set up
818

919
> Either follow along below to create your sample Project or clone the repo from [Github](https://github.com/mjhea0/django-tastypie-tutorial).
1020
11-
Create a new directory, setup and activate virtualenv, install Django and the required dependencies:
21+
Create a new project directory, create and activate a virtualenv, install Django and the required dependencies:
1222

1323
```sh
1424
$ mkdir django-tastypie-tutorial
1525
$ cd django-tastypie-tutorial
16-
$ virtualenv --no-site-packages env
26+
$ pyvenv-3.5 env
1727
$ source env/bin/activate
1828
$ pip install Django==1.9.7
1929
$ pip install django-tastypie==0.13.3
@@ -29,24 +39,37 @@ $ cd django19
2939
$ python manage.py startapp whatever
3040
```
3141

32-
> Make sure to add the app to your `INSTALLED_APPS` section in *settings.py*.
42+
Make sure to add the app to your `INSTALLED_APPS` section in *settings.py*:
43+
44+
```python
45+
INSTALLED_APPS = [
46+
'django.contrib.admin',
47+
'django.contrib.auth',
48+
'django.contrib.contenttypes',
49+
'django.contrib.sessions',
50+
'django.contrib.messages',
51+
'django.contrib.staticfiles',
52+
'whatever',
53+
]
54+
```
3355

3456
Add support for SQLite (or your RDBMS of choice) in *settings.py*:
3557

3658
```python
3759
DATABASES = {
3860
'default': {
3961
'ENGINE': 'django.db.backends.sqlite3',
40-
'NAME': 'test.db',
62+
'NAME': os.path.join(BASE_DIR, 'test.db'),
4163
}
4264
}
4365
```
4466

45-
Enable the Django Admin, then update your *models.py* file:
67+
Update your *models.py* file:
4668

4769
```python
4870
from django.db import models
4971

72+
5073
class Whatever(models.Model):
5174
title = models.CharField(max_length=200)
5275
body = models.TextField()
@@ -68,7 +91,7 @@ Now migrate them:
6891
$ python manage.py migrate --fake-initial
6992
```
7093

71-
**Note** The `fake-initial` optional argument is required if we have to troublehsoot the existing migrations if any. Please omit if no migrations exist already.
94+
> **Note**: The `fake-initial` optional argument is required if you have to troubleshoot the existing migrations. Omit if no migrations exist.
7295
7396
Fire up the Django Shell and populate the database:
7497

@@ -85,56 +108,63 @@ $ python manage.py shell
85108
>>> w.save()
86109
```
87110

88-
## Setup Tasypie
111+
Exit the shell when done.
112+
113+
## Tastypie set up
89114

90-
Create a new file in your Project called *api.py*.
115+
Create a new file in your App called *api.py*.
91116

92117
```python
93118
from tastypie.resources import ModelResource
94119
from tastypie.constants import ALL
95-
from models import Whatever
120+
121+
from whatever.models import Whatever
122+
96123

97124
class WhateverResource(ModelResource):
98125
class Meta:
99126
queryset = Whatever.objects.all()
100127
resource_name = 'whatever'
101-
filtering = { "title" : ALL }
128+
filtering = {'title': ALL}
102129
```
103130

104131
Update *urls.py*:
105132

106133
```python
107-
from django.conf.urls import patterns, include, url
108-
from .api import WhateverResource
134+
from django.conf.urls import url, include
135+
from django.contrib import admin
136+
137+
from django19.api import WhateverResource
109138

110139
whatever_resource = WhateverResource()
111140

112-
urlpatterns = patterns('',
113-
url(r'^api/', include(whatever_resource.urls)),
114-
)
141+
urlpatterns = [
142+
url(r'^admin/', admin.site.urls),
143+
url(r'^api/', include(whatever_resource.urls)),
144+
]
115145
```
116146

117-
## Fire Away!
147+
## Fire away!
118148

119149
1. Fire up the server.
120-
2. Navigate to [http://localhost:8000/api/whatever/?format=json](http://localhost:8000/api/whatever/?format=json) to get the data in JSON format
121-
3. Navigate to [http://localhost:8000/api/whatever/?format=xml](http://localhost:8000/api/whatever/?format=json) to get the data in XML format
150+
1. Navigate to [http://localhost:8000/api/whatever/?format=json](http://localhost:8000/api/whatever/?format=json) to get the data in JSON format
151+
1. Navigate to [http://localhost:8000/api/whatever/?format=xml](http://localhost:8000/api/whatever/?format=json) to get the data in XML format
122152

123-
Remember the filer we put on the `WhateverResource` class?
153+
Remember the filter we put on the `WhateverResource` class?
124154

125155
```python
126-
filtering = { "title" : ALL }
156+
filtering = {'title': ALL}
127157
```
128158

129159
Well, we can filter the objects by title. Try out various keywords:
130160

131-
1. [http://localhost:8000/api/whatever/?format=xml&title__contains=what](http://localhost:8000/api/whatever/?format=xml&title__contains=what)
132-
1. [http://localhost:8000/api/whatever/?format=xml&title__contains=test](http://localhost:8000/api/whatever/?format=xml&title__contains=test)
161+
1. [http://localhost:8000/api/whatever/?format=json&title__contains=what](http://localhost:8000/api/whatever/?format=json&title__contains=what)
162+
1. [http://localhost:8000/api/whatever/?format=json&title__contains=test](http://localhost:8000/api/whatever/?format=json&title__contains=test)
133163

134-
Simple, right!?
164+
Simple, right!?!
135165

136166
***
137167

138-
There is so much more that can configured with Tastypie. Check out the official [docs](http://tastypieapi.org/) if you need more info. Comment below if you have questions.
168+
There is so much more that can configured with Tastypie. Check out the official [docs](http://tastypieapi.org/) for more info. Comment below if you have questions.
139169

140-
Again, you can download the code [here](https://github.com/mjhea0/django-tastypie-tutorial).
170+
Again, you can download the code from the [repo](https://github.com/mjhea0/django-tastypie-tutorial).

2016/2016-07-06-testing-third-party-apis-with-mock-servers.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ class TestMockServer(object):
259259
assert_list_equal(response.json(), [])
260260
```
261261

262-
Notice a new technique being used in the `test_mock_server.py` code. The `response = get_users()` line is wrapped with a `patch.dict()` function from the *mock* library.
262+
Notice a new technique being used in the *test_mock_server.py* code. The `response = get_users()` line is wrapped with a `patch.dict()` function from the *mock* library.
263263

264264
What does this statement do?
265265

0 commit comments

Comments
 (0)