Add basic Baby and Sleep models.

This commit is contained in:
Christopher Charbonneau Wells 2017-08-13 10:48:16 -04:00
parent fbbaa76153
commit cafbe048cb
9 changed files with 87 additions and 12 deletions

View File

@ -1,3 +0,0 @@
from django.contrib import admin
# Register your models here.

View File

@ -1,3 +0,0 @@
from django.db import models
# Create your models here.

View File

@ -4,8 +4,22 @@ from __future__ import unicode_literals
from django.contrib.auth.models import User
from rest_framework import serializers
from core.models import Baby, Sleep
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ('url', 'username', 'email', 'is_staff')
class BabySerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Baby
fields = ('first_name', 'last_name', 'birth_date')
class SleepSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Sleep
fields = ('baby', 'start', 'end')

View File

@ -1,11 +1,13 @@
from django.conf.urls import url, include
from rest_framework import routers
from .views import UserViewSet
from .views import UserViewSet, BabyViewSet, SleepViewSet
# Routers provide an easy way of automatically determining the URL conf.
router = routers.DefaultRouter()
router.register(r'users', UserViewSet)
router.register(r'babies', BabyViewSet)
router.register(r'sleep', SleepViewSet)
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.

View File

@ -4,9 +4,20 @@ from __future__ import unicode_literals
from django.contrib.auth.models import User
from rest_framework import viewsets
from .serializers import (UserSerializer)
from core.models import Baby, Sleep
from .serializers import (UserSerializer, BabySerializer, SleepSerializer)
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
class BabyViewSet(viewsets.ModelViewSet):
queryset = Baby.objects.all()
serializer_class = BabySerializer
class SleepViewSet(viewsets.ModelViewSet):
queryset = Sleep.objects.all()
serializer_class = SleepSerializer

View File

@ -31,13 +31,17 @@ ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'api',
'core',
'rest_framework',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework'
]
MIDDLEWARE = [

View File

@ -1,3 +1,18 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib import admin
# Register your models here.
from .models import Baby, Sleep
@admin.register(Baby)
class BabyAdmin(admin.ModelAdmin):
list_display = ('first_name', 'last_name', 'birth_date',)
search_fields = ('first_name', 'last_name', 'birth_date',)
@admin.register(Sleep)
class SleepAdmin(admin.ModelAdmin):
list_display = ('baby', 'start', 'end',)
search_fields = ('baby__first_name', 'baby__last_name',)

View File

@ -1,3 +1,38 @@
from django.db import models
# Create your models here.
class Baby(models.Model):
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
birth_date = models.DateField(blank=False, null=False)
objects = models.Manager()
class Meta:
default_permissions = ('view', 'add', 'change', 'delete')
ordering = ['last_name', 'first_name']
verbose_name_plural = 'Babies'
def __str__(self):
return '{} {}'.format(self.first_name, self.last_name)
class Sleep(models.Model):
baby = models.ForeignKey('Baby', related_name='sleep')
start = models.DateTimeField(blank=False, null=False)
end = models.DateTimeField(blank=False, null=False)
objects = models.Manager()
class Meta:
default_permissions = ('view', 'add', 'change', 'delete')
ordering = ['-start']
verbose_name_plural = 'Sleep'
def __str__(self):
return '{} slept from {} to {} on {}'.format(
self.baby,
self.start.time(),
self.end.time(),
self.end.date(),
)

Binary file not shown.