commit
848b568b2f
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,6 @@
|
|||||||
|
from django.contrib import admin
|
||||||
|
|
||||||
|
# Register your models here.
|
||||||
|
from .models import Company
|
||||||
|
admin.site.register(Company)
|
||||||
|
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
from django.apps import AppConfig
|
||||||
|
|
||||||
|
|
||||||
|
class CompaniesApiConfig(AppConfig):
|
||||||
|
name = 'companies_api'
|
||||||
@ -0,0 +1,22 @@
|
|||||||
|
# Generated by Django 3.1.7 on 2021-03-10 00:34
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
initial = True
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='Company',
|
||||||
|
fields=[
|
||||||
|
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('name', models.CharField(max_length=32)),
|
||||||
|
('industry', models.CharField(max_length=32)),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
||||||
@ -0,0 +1,20 @@
|
|||||||
|
# Generated by Django 3.1.7 on 2021-03-10 21:39
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
import django.db.models.deletion
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('locations_api', '0001_initial'),
|
||||||
|
('companies_api', '0001_initial'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='company',
|
||||||
|
name='home',
|
||||||
|
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='companies', to='locations_api.location'),
|
||||||
|
),
|
||||||
|
]
|
||||||
@ -0,0 +1,18 @@
|
|||||||
|
# Generated by Django 3.1.7 on 2021-03-10 21:42
|
||||||
|
|
||||||
|
from django.db import migrations
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('companies_api', '0002_company_home'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.RenameField(
|
||||||
|
model_name='company',
|
||||||
|
old_name='home',
|
||||||
|
new_name='headquarters',
|
||||||
|
),
|
||||||
|
]
|
||||||
@ -0,0 +1,19 @@
|
|||||||
|
# Generated by Django 3.1.7 on 2021-03-11 21:02
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('contacts_api', '0002_contact_home'),
|
||||||
|
('companies_api', '0003_auto_20210310_2142'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='company',
|
||||||
|
name='employees',
|
||||||
|
field=models.ManyToManyField(to='contacts_api.Contact'),
|
||||||
|
),
|
||||||
|
]
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,10 @@
|
|||||||
|
from django.db import models
|
||||||
|
from locations_api.models import Location
|
||||||
|
from contacts_api.models import Contact
|
||||||
|
|
||||||
|
# Create your models here.
|
||||||
|
class Company(models.Model):
|
||||||
|
name = models.CharField(max_length=32)
|
||||||
|
industry = models.CharField(max_length=32)
|
||||||
|
headquarters = models.ForeignKey(Location, related_name='companies', null=True, on_delete=models.SET_NULL)
|
||||||
|
employees = models.ManyToManyField(Contact)
|
||||||
@ -0,0 +1,10 @@
|
|||||||
|
from .serializers_base import CompanySerializerBase
|
||||||
|
from locations_api.serializers_base import LocationSerializerBase
|
||||||
|
from contacts_api.serializers import ContactSerializer
|
||||||
|
|
||||||
|
class CompanySerializer(CompanySerializerBase): # serializers.ModelSerializer just tells django to convert sql to JSON
|
||||||
|
headquarters = LocationSerializerBase()
|
||||||
|
employees = ContactSerializer(many=True)
|
||||||
|
|
||||||
|
class Meta(CompanySerializerBase.Meta):
|
||||||
|
fields = CompanySerializerBase.Meta.fields + ('headquarters', 'employees')
|
||||||
@ -0,0 +1,15 @@
|
|||||||
|
from rest_framework import serializers
|
||||||
|
from locations_api.serializers_base import LocationSerializerBase
|
||||||
|
from .models import Company
|
||||||
|
|
||||||
|
class CompanySerializerBase(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = Company# tell django which model to use
|
||||||
|
fields = ('id', 'name', 'industry',) # tell django which fields to include
|
||||||
|
|
||||||
|
class CompanySerializerWithHeadquarters(CompanySerializerBase):
|
||||||
|
headquarters = LocationSerializerBase()
|
||||||
|
|
||||||
|
class Meta(CompanySerializerBase.Meta):
|
||||||
|
fields = CompanySerializerBase.Meta.fields + ('headquarters',)
|
||||||
|
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
from django.test import TestCase
|
||||||
|
|
||||||
|
# Create your tests here.
|
||||||
@ -0,0 +1,8 @@
|
|||||||
|
from django.urls import path
|
||||||
|
from . import views
|
||||||
|
|
||||||
|
urlpatterns = [
|
||||||
|
path('api/companies', views.CompanyList.as_view(), name='company_list'), # api/contacts will be routed to the ContactList view for handling
|
||||||
|
path('api/companies/<int:pk>', views.CompanyDetail.as_view(), name='company_detail'), # api/contacts will be routed to the ContactDetail view for handling
|
||||||
|
]
|
||||||
|
|
||||||
@ -0,0 +1,12 @@
|
|||||||
|
from rest_framework import generics
|
||||||
|
from .serializers import CompanySerializer
|
||||||
|
from .models import Company
|
||||||
|
|
||||||
|
class CompanyList(generics.ListCreateAPIView):
|
||||||
|
queryset = Company.objects.all() # tell django how to retrieve all objects from the DB
|
||||||
|
serializer_class = CompanySerializer # tell django what serializer to use
|
||||||
|
|
||||||
|
class CompanyDetail(generics.RetrieveUpdateDestroyAPIView):
|
||||||
|
queryset = Company.objects.all()
|
||||||
|
serializer_class = CompanySerializer
|
||||||
|
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,6 @@
|
|||||||
|
from django.contrib import admin
|
||||||
|
|
||||||
|
# Register your models here.
|
||||||
|
from .models import Contact
|
||||||
|
admin.site.register(Contact)
|
||||||
|
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
from django.apps import AppConfig
|
||||||
|
|
||||||
|
|
||||||
|
class ContactsApiConfig(AppConfig):
|
||||||
|
name = 'contacts_api'
|
||||||
@ -0,0 +1,22 @@
|
|||||||
|
# Generated by Django 3.1.7 on 2021-03-09 21:57
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
initial = True
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='Contact',
|
||||||
|
fields=[
|
||||||
|
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('name', models.CharField(max_length=32)),
|
||||||
|
('age', models.IntegerField()),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
||||||
@ -0,0 +1,20 @@
|
|||||||
|
# Generated by Django 3.1.7 on 2021-03-10 21:55
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
import django.db.models.deletion
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('locations_api', '0001_initial'),
|
||||||
|
('contacts_api', '0001_initial'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='contact',
|
||||||
|
name='home',
|
||||||
|
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='homes', to='locations_api.location'),
|
||||||
|
),
|
||||||
|
]
|
||||||
@ -0,0 +1,20 @@
|
|||||||
|
# Generated by Django 3.1.7 on 2021-03-11 21:02
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
import django.db.models.deletion
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('locations_api', '0001_initial'),
|
||||||
|
('contacts_api', '0002_contact_home'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='contact',
|
||||||
|
name='home',
|
||||||
|
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='inhabitants', to='locations_api.location'),
|
||||||
|
),
|
||||||
|
]
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,9 @@
|
|||||||
|
from django.db import models
|
||||||
|
from locations_api.models import Location
|
||||||
|
|
||||||
|
# Create your models here.
|
||||||
|
class Contact(models.Model):
|
||||||
|
name = models.CharField(max_length=32)
|
||||||
|
age = models.IntegerField()
|
||||||
|
home = models.ForeignKey(Location, related_name='inhabitants', null=True, on_delete=models.SET_NULL)
|
||||||
|
|
||||||
@ -0,0 +1,10 @@
|
|||||||
|
from .serializers_base import ContactSerializerBase
|
||||||
|
from locations_api.serializers_base import LocationSerializerBase
|
||||||
|
from companies_api.serializers_base import CompanySerializerWithHeadquarters
|
||||||
|
|
||||||
|
class ContactSerializer(ContactSerializerBase): # serializers.ModelSerializer just tells django to convert sql to JSON
|
||||||
|
home = LocationSerializerBase()
|
||||||
|
company_set = CompanySerializerWithHeadquarters(many=True)
|
||||||
|
|
||||||
|
class Meta(ContactSerializerBase.Meta):
|
||||||
|
fields = ContactSerializerBase.Meta.fields + ('home', 'company_set')
|
||||||
@ -0,0 +1,8 @@
|
|||||||
|
from rest_framework import serializers
|
||||||
|
from .models import Contact
|
||||||
|
|
||||||
|
class ContactSerializerBase(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = Contact # tell django which model to use
|
||||||
|
fields = ('id', 'name', 'age',) # tell django which fields to include
|
||||||
|
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
from django.test import TestCase
|
||||||
|
|
||||||
|
# Create your tests here.
|
||||||
@ -0,0 +1,7 @@
|
|||||||
|
from django.urls import path
|
||||||
|
from . import views
|
||||||
|
|
||||||
|
urlpatterns = [
|
||||||
|
path('api/contacts', views.ContactList.as_view(), name='contact_list'), # api/contacts will be routed to the ContactList view for handling
|
||||||
|
path('api/contacts/<int:pk>', views.ContactDetail.as_view(), name='contact_detail'), # api/contacts will be routed to the ContactDetail view for handling
|
||||||
|
]
|
||||||
@ -0,0 +1,12 @@
|
|||||||
|
from rest_framework import generics
|
||||||
|
from .serializers import ContactSerializer
|
||||||
|
from .models import Contact
|
||||||
|
|
||||||
|
class ContactList(generics.ListCreateAPIView):
|
||||||
|
queryset = Contact.objects.all() # tell django how to retrieve all objects from the DB
|
||||||
|
serializer_class = ContactSerializer # tell django what serializer to use
|
||||||
|
|
||||||
|
class ContactDetail(generics.RetrieveUpdateDestroyAPIView):
|
||||||
|
queryset = Contact.objects.all()
|
||||||
|
serializer_class = ContactSerializer
|
||||||
|
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,16 @@
|
|||||||
|
"""
|
||||||
|
ASGI config for django_rest_api project.
|
||||||
|
|
||||||
|
It exposes the ASGI callable as a module-level variable named ``application``.
|
||||||
|
|
||||||
|
For more information on this file, see
|
||||||
|
https://docs.djangoproject.com/en/3.1/howto/deployment/asgi/
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
from django.core.asgi import get_asgi_application
|
||||||
|
|
||||||
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_rest_api.settings')
|
||||||
|
|
||||||
|
application = get_asgi_application()
|
||||||
@ -0,0 +1,130 @@
|
|||||||
|
"""
|
||||||
|
Django settings for django_rest_api project.
|
||||||
|
|
||||||
|
Generated by 'django-admin startproject' using Django 3.1.7.
|
||||||
|
|
||||||
|
For more information on this file, see
|
||||||
|
https://docs.djangoproject.com/en/3.1/topics/settings/
|
||||||
|
|
||||||
|
For the full list of settings and their values, see
|
||||||
|
https://docs.djangoproject.com/en/3.1/ref/settings/
|
||||||
|
"""
|
||||||
|
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
||||||
|
BASE_DIR = Path(__file__).resolve().parent.parent
|
||||||
|
|
||||||
|
|
||||||
|
# Quick-start development settings - unsuitable for production
|
||||||
|
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/
|
||||||
|
|
||||||
|
# SECURITY WARNING: keep the secret key used in production secret!
|
||||||
|
SECRET_KEY = '(12(paz%k=r92df76nmdt$(cwdsw7k7=!*2vvd55ad+p%112!%'
|
||||||
|
|
||||||
|
# SECURITY WARNING: don't run with debug turned on in production!
|
||||||
|
DEBUG = True
|
||||||
|
|
||||||
|
ALLOWED_HOSTS = []
|
||||||
|
|
||||||
|
|
||||||
|
# Application definition
|
||||||
|
|
||||||
|
INSTALLED_APPS = [
|
||||||
|
'corsheaders', # add this
|
||||||
|
'contacts_api', # add this
|
||||||
|
'locations_api',
|
||||||
|
'companies_api',
|
||||||
|
'rest_framework', # add this
|
||||||
|
'django.contrib.admin',
|
||||||
|
'django.contrib.auth',
|
||||||
|
'django.contrib.contenttypes',
|
||||||
|
'django.contrib.sessions',
|
||||||
|
'django.contrib.messages',
|
||||||
|
'django.contrib.staticfiles',
|
||||||
|
]
|
||||||
|
|
||||||
|
MIDDLEWARE = [
|
||||||
|
'corsheaders.middleware.CorsMiddleware', # this makes the cors package run for all requests. A bit like app.use() in express
|
||||||
|
'django.middleware.security.SecurityMiddleware',
|
||||||
|
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||||
|
'django.middleware.common.CommonMiddleware',
|
||||||
|
'django.middleware.csrf.CsrfViewMiddleware',
|
||||||
|
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||||
|
'django.contrib.messages.middleware.MessageMiddleware',
|
||||||
|
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||||
|
]
|
||||||
|
CORS_ALLOW_ALL_ORIGINS = True # add this
|
||||||
|
|
||||||
|
ROOT_URLCONF = 'django_rest_api.urls'
|
||||||
|
|
||||||
|
TEMPLATES = [
|
||||||
|
{
|
||||||
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
||||||
|
'DIRS': [],
|
||||||
|
'APP_DIRS': True,
|
||||||
|
'OPTIONS': {
|
||||||
|
'context_processors': [
|
||||||
|
'django.template.context_processors.debug',
|
||||||
|
'django.template.context_processors.request',
|
||||||
|
'django.contrib.auth.context_processors.auth',
|
||||||
|
'django.contrib.messages.context_processors.messages',
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
WSGI_APPLICATION = 'django_rest_api.wsgi.application'
|
||||||
|
|
||||||
|
|
||||||
|
# Database
|
||||||
|
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
|
||||||
|
|
||||||
|
DATABASES = {
|
||||||
|
'default': {
|
||||||
|
'ENGINE': 'django.db.backends.postgresql',
|
||||||
|
'NAME': 'django_api',
|
||||||
|
'USER': '',
|
||||||
|
'PASSWORD': '',
|
||||||
|
'HOST': 'localhost'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Password validation
|
||||||
|
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators
|
||||||
|
|
||||||
|
AUTH_PASSWORD_VALIDATORS = [
|
||||||
|
{
|
||||||
|
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
# Internationalization
|
||||||
|
# https://docs.djangoproject.com/en/3.1/topics/i18n/
|
||||||
|
|
||||||
|
LANGUAGE_CODE = 'en-us'
|
||||||
|
|
||||||
|
TIME_ZONE = 'UTC'
|
||||||
|
|
||||||
|
USE_I18N = True
|
||||||
|
|
||||||
|
USE_L10N = True
|
||||||
|
|
||||||
|
USE_TZ = True
|
||||||
|
|
||||||
|
|
||||||
|
# Static files (CSS, JavaScript, Images)
|
||||||
|
# https://docs.djangoproject.com/en/3.1/howto/static-files/
|
||||||
|
|
||||||
|
STATIC_URL = '/static/'
|
||||||
@ -0,0 +1,25 @@
|
|||||||
|
"""django_rest_api URL Configuration
|
||||||
|
|
||||||
|
The `urlpatterns` list routes URLs to views. For more information please see:
|
||||||
|
https://docs.djangoproject.com/en/3.1/topics/http/urls/
|
||||||
|
Examples:
|
||||||
|
Function views
|
||||||
|
1. Add an import: from my_app import views
|
||||||
|
2. Add a URL to urlpatterns: path('', views.home, name='home')
|
||||||
|
Class-based views
|
||||||
|
1. Add an import: from other_app.views import Home
|
||||||
|
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
|
||||||
|
Including another URLconf
|
||||||
|
1. Import the include() function: from django.urls import include, path
|
||||||
|
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
||||||
|
"""
|
||||||
|
from django.contrib import admin
|
||||||
|
from django.urls import path
|
||||||
|
from django.conf.urls import include # add this
|
||||||
|
|
||||||
|
urlpatterns = [
|
||||||
|
path('', include('contacts_api.urls')),
|
||||||
|
path('', include('locations_api.urls')),
|
||||||
|
path('', include('companies_api.urls')),
|
||||||
|
path('admin/', admin.site.urls),
|
||||||
|
]
|
||||||
@ -0,0 +1,16 @@
|
|||||||
|
"""
|
||||||
|
WSGI config for django_rest_api project.
|
||||||
|
|
||||||
|
It exposes the WSGI callable as a module-level variable named ``application``.
|
||||||
|
|
||||||
|
For more information on this file, see
|
||||||
|
https://docs.djangoproject.com/en/3.1/howto/deployment/wsgi/
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
from django.core.wsgi import get_wsgi_application
|
||||||
|
|
||||||
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_rest_api.settings')
|
||||||
|
|
||||||
|
application = get_wsgi_application()
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,6 @@
|
|||||||
|
from django.contrib import admin
|
||||||
|
|
||||||
|
# Register your models here.
|
||||||
|
from .models import Location
|
||||||
|
admin.site.register(Location)
|
||||||
|
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
from django.apps import AppConfig
|
||||||
|
|
||||||
|
|
||||||
|
class LocationsApiConfig(AppConfig):
|
||||||
|
name = 'locations_api'
|
||||||
@ -0,0 +1,23 @@
|
|||||||
|
# Generated by Django 3.1.7 on 2021-03-10 05:12
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
initial = True
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='Location',
|
||||||
|
fields=[
|
||||||
|
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('street', models.CharField(max_length=32)),
|
||||||
|
('city', models.CharField(max_length=32)),
|
||||||
|
('state', models.CharField(max_length=2)),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
||||||
Binary file not shown.
Binary file not shown.
@ -0,0 +1,7 @@
|
|||||||
|
from django.db import models
|
||||||
|
|
||||||
|
# Create your models here.
|
||||||
|
class Location(models.Model):
|
||||||
|
street = models.CharField(max_length=32)
|
||||||
|
city = models.CharField(max_length=32)
|
||||||
|
state = models.CharField(max_length=2)
|
||||||
@ -0,0 +1,10 @@
|
|||||||
|
from .serializers_base import LocationSerializerBase
|
||||||
|
from contacts_api.serializers_base import ContactSerializerBase
|
||||||
|
from companies_api.serializers_base import CompanySerializerBase
|
||||||
|
|
||||||
|
class LocationSerializer(LocationSerializerBase): # serializers.ModelSerializer just tells django to convert sql to JSON
|
||||||
|
inhabitants = ContactSerializerBase(many=True)
|
||||||
|
companies = CompanySerializerBase(many=True)
|
||||||
|
|
||||||
|
class Meta(LocationSerializerBase.Meta):
|
||||||
|
fields = LocationSerializerBase.Meta.fields + ('inhabitants', 'companies')
|
||||||
@ -0,0 +1,7 @@
|
|||||||
|
from rest_framework import serializers
|
||||||
|
from .models import Location
|
||||||
|
|
||||||
|
class LocationSerializerBase(serializers.ModelSerializer): # serializers.ModelSerializer just tells django to convert sql to JSON
|
||||||
|
class Meta:
|
||||||
|
model = Location # tell django which model to use
|
||||||
|
fields = ('id', 'street', 'city', 'state')
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
from django.test import TestCase
|
||||||
|
|
||||||
|
# Create your tests here.
|
||||||
@ -0,0 +1,7 @@
|
|||||||
|
from django.urls import path
|
||||||
|
from . import views
|
||||||
|
|
||||||
|
urlpatterns = [
|
||||||
|
path('api/locations', views.LocationList.as_view(), name='location_list'), # api/contacts will be routed to the ContactList view for handling
|
||||||
|
path('api/locations/<int:pk>', views.LocationDetail.as_view(), name='location_detail'), # api/contacts will be routed to the ContactDetail view for handling
|
||||||
|
]
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
from rest_framework import generics
|
||||||
|
from .serializers import LocationSerializer
|
||||||
|
from .models import Location
|
||||||
|
|
||||||
|
class LocationList(generics.ListCreateAPIView):
|
||||||
|
queryset = Location.objects.all() # tell django how to retrieve all objects from the DB
|
||||||
|
serializer_class = LocationSerializer # tell django what serializer to use
|
||||||
|
|
||||||
|
class LocationDetail(generics.RetrieveUpdateDestroyAPIView):
|
||||||
|
queryset = Location.objects.all()
|
||||||
|
serializer_class = LocationSerializer
|
||||||
@ -0,0 +1,22 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
"""Django's command-line utility for administrative tasks."""
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
"""Run administrative tasks."""
|
||||||
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_rest_api.settings')
|
||||||
|
try:
|
||||||
|
from django.core.management import execute_from_command_line
|
||||||
|
except ImportError as exc:
|
||||||
|
raise ImportError(
|
||||||
|
"Couldn't import Django. Are you sure it's installed and "
|
||||||
|
"available on your PYTHONPATH environment variable? Did you "
|
||||||
|
"forget to activate a virtual environment?"
|
||||||
|
) from exc
|
||||||
|
execute_from_command_line(sys.argv)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
Loading…
Reference in new issue