You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 2013/2013-07-29-create-a-super-basic-rest-api-with-django-tastypie.markdown
+59-29Lines changed: 59 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff 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
2
2
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/).
4
4
5
-
> **Note:** This tutorial is using Python 3.5 and the latest Django 1.9.7.
5
+
*Updates:*
6
6
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)).
> Either follow along below to create your sample Project or clone the repo from [Github](https://github.com/mjhea0/django-tastypie-tutorial).
10
20
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:
12
22
13
23
```sh
14
24
$ mkdir django-tastypie-tutorial
15
25
$ cd django-tastypie-tutorial
16
-
$ virtualenv --no-site-packages env
26
+
$ pyvenv-3.5 env
17
27
$ source env/bin/activate
18
28
$ pip install Django==1.9.7
19
29
$ pip install django-tastypie==0.13.3
@@ -29,24 +39,37 @@ $ cd django19
29
39
$ python manage.py startapp whatever
30
40
```
31
41
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
+
```
33
55
34
56
Add support for SQLite (or your RDBMS of choice) in *settings.py*:
35
57
36
58
```python
37
59
DATABASES= {
38
60
'default': {
39
61
'ENGINE': 'django.db.backends.sqlite3',
40
-
'NAME': 'test.db',
62
+
'NAME': os.path.join(BASE_DIR, 'test.db'),
41
63
}
42
64
}
43
65
```
44
66
45
-
Enable the Django Admin, then update your *models.py* file:
67
+
Update your *models.py* file:
46
68
47
69
```python
48
70
from django.db import models
49
71
72
+
50
73
classWhatever(models.Model):
51
74
title = models.CharField(max_length=200)
52
75
body = models.TextField()
@@ -68,7 +91,7 @@ Now migrate them:
68
91
$ python manage.py migrate --fake-initial
69
92
```
70
93
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.
72
95
73
96
Fire up the Django Shell and populate the database:
74
97
@@ -85,56 +108,63 @@ $ python manage.py shell
85
108
>>> w.save()
86
109
```
87
110
88
-
## Setup Tasypie
111
+
Exit the shell when done.
112
+
113
+
## Tastypie set up
89
114
90
-
Create a new file in your Project called *api.py*.
115
+
Create a new file in your App called *api.py*.
91
116
92
117
```python
93
118
from tastypie.resources import ModelResource
94
119
from tastypie.constants importALL
95
-
from models import Whatever
120
+
121
+
from whatever.models import Whatever
122
+
96
123
97
124
classWhateverResource(ModelResource):
98
125
classMeta:
99
126
queryset = Whatever.objects.all()
100
127
resource_name ='whatever'
101
-
filtering = {"title": ALL}
128
+
filtering = {'title': ALL}
102
129
```
103
130
104
131
Update *urls.py*:
105
132
106
133
```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
109
138
110
139
whatever_resource = WhateverResource()
111
140
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
+
]
115
145
```
116
146
117
-
## Fire Away!
147
+
## Fire away!
118
148
119
149
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
122
152
123
-
Remember the filer we put on the `WhateverResource` class?
153
+
Remember the filter we put on the `WhateverResource` class?
124
154
125
155
```python
126
-
filtering = {"title": ALL}
156
+
filtering = {'title': ALL}
127
157
```
128
158
129
159
Well, we can filter the objects by title. Try out various keywords:
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.
139
169
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).
Copy file name to clipboardExpand all lines: 2016/2016-07-06-testing-third-party-apis-with-mock-servers.markdown
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -259,7 +259,7 @@ class TestMockServer(object):
259
259
assert_list_equal(response.json(), [])
260
260
```
261
261
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.
0 commit comments