Skip to content

Commit 9bbf08d

Browse files
committed
Python: Add simple Django REST framework code
1 parent 9d84315 commit 9bbf08d

File tree

10 files changed

+149
-12
lines changed

10 files changed

+149
-12
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
db.sqlite3
Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,23 @@
1-
See README for `django-v2-v3` which described how the project was set up and how to run the development server.
1+
See README for `django-v2-v3` which described how the project was set up.
2+
3+
Since this test project uses models (and a DB), you generally need to run there 3 commands:
4+
5+
```
6+
python manage.py makemigrations
7+
python manage.py migrate
8+
python manage.py runserver
9+
```
10+
11+
Then visit http://127.0.0.1:8000/
12+
13+
# References
14+
15+
- https://www.django-rest-framework.org/tutorial/quickstart/
16+
17+
# Editing data
18+
19+
To edit data you should add an admin user (will prompt for password)
20+
21+
```
22+
python manage.py createsuperuser --email [email protected] --username admin
23+
```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
from .models import Foo, Bar
2+
13
from django.contrib import admin
24

35
# Register your models here.
6+
7+
admin.site.register(Foo)
8+
admin.site.register(Bar)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Generated by Django 3.2.8 on 2021-10-27 11:54
2+
3+
from django.db import migrations, models
4+
import django.db.models.deletion
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
initial = True
10+
11+
dependencies = [
12+
]
13+
14+
operations = [
15+
migrations.CreateModel(
16+
name='Foo',
17+
fields=[
18+
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
19+
('title', models.CharField(max_length=100)),
20+
('field_not_displayed', models.IntegerField()),
21+
],
22+
),
23+
migrations.CreateModel(
24+
name='Bar',
25+
fields=[
26+
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
27+
('n', models.IntegerField()),
28+
('foo', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='testapp.foo')),
29+
],
30+
),
31+
]
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Generated by Django 3.2.8 on 2021-10-27 12:06
2+
3+
from django.db import migrations
4+
5+
def add_dummy_data(apps, schema_editor):
6+
Foo = apps.get_model("testapp", "Foo")
7+
Bar = apps.get_model("testapp", "Bar")
8+
9+
f1 = Foo(title="example 1", field_not_displayed=10)
10+
f1.save()
11+
f2 = Foo(title="example 2", field_not_displayed=20)
12+
f2.save()
13+
14+
b1 = Bar(n=42, foo=f1)
15+
b1.save()
16+
b2 = Bar(n=43, foo=f1)
17+
b2.save()
18+
b3 = Bar(n=1000, foo=f2)
19+
b3.save()
20+
21+
class Migration(migrations.Migration):
22+
23+
dependencies = [
24+
('testapp', '0001_initial'),
25+
]
26+
27+
operations = [
28+
migrations.RunPython(add_dummy_data),
29+
]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
from django.db import models
22

33
# Create your models here.
4+
5+
6+
class Foo(models.Model):
7+
title = models.CharField(max_length=100)
8+
field_not_displayed = models.IntegerField()
9+
10+
11+
class Bar(models.Model):
12+
n = models.IntegerField()
13+
foo = models.ForeignKey(Foo, on_delete=models.PROTECT)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from .models import Foo, Bar
2+
from rest_framework import serializers
3+
4+
5+
class FooSerializer(serializers.HyperlinkedModelSerializer):
6+
class Meta:
7+
model = Foo
8+
fields = ["title"]
9+
10+
11+
class BarSerializer(serializers.HyperlinkedModelSerializer):
12+
class Meta:
13+
model = Bar
14+
fields = ["n", "foo"]
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
from django.urls import path
1+
from django.urls import path, include
2+
from rest_framework import routers
23

34
from . import views
45

6+
7+
router = routers.DefaultRouter()
8+
router.register(r"foos", views.FooViewSet)
9+
router.register(r"bars", views.BarViewSet)
10+
511
urlpatterns = [
6-
path("foo/", views.foo), # $routeSetup="foo/"
12+
path("", include(router.urls)),
13+
path("api-auth/", include("rest_framework.urls", namespace="rest_framework")),
14+
path("example/", views.example), # $routeSetup="example/"
715
]
Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
1-
from django.http import HttpResponse, HttpRequest
1+
from .models import Foo, Bar
2+
from .serializers import FooSerializer, BarSerializer
3+
4+
from rest_framework import viewsets
5+
6+
7+
class FooViewSet(viewsets.ModelViewSet):
8+
queryset = Foo.objects.all()
9+
serializer_class = FooSerializer
210

3-
def foo(request: HttpRequest):
4-
return HttpResponse("foo")
11+
12+
class BarViewSet(viewsets.ModelViewSet):
13+
queryset = Bar.objects.all()
14+
serializer_class = BarSerializer
15+
16+
17+
# this is pure django
18+
from django.http import HttpResponse, HttpRequest
19+
def example(request: HttpRequest):
20+
return HttpResponse("example")

python/ql/test/library-tests/frameworks/rest_framework/testproj/settings.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
'django.contrib.sessions',
3939
'django.contrib.messages',
4040
'django.contrib.staticfiles',
41+
'rest_framework',
4142
]
4243

4344
MIDDLEWARE = [
@@ -74,12 +75,12 @@
7475
# Database
7576
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
7677

77-
# DATABASES = {
78-
# 'default': {
79-
# 'ENGINE': 'django.db.backends.sqlite3',
80-
# 'NAME': BASE_DIR / 'db.sqlite3',
81-
# }
82-
# }
78+
DATABASES = {
79+
'default': {
80+
'ENGINE': 'django.db.backends.sqlite3',
81+
'NAME': BASE_DIR / 'db.sqlite3',
82+
}
83+
}
8384

8485

8586
# Password validation

0 commit comments

Comments
 (0)