Add slug field to Child.

This commit is contained in:
Christopher Charbonneau Wells 2017-08-18 08:08:23 -04:00
parent 8772f87d8a
commit 37f28c3ff7
4 changed files with 11 additions and 3 deletions

View File

@ -10,7 +10,8 @@ from core.models import (Child, DiaperChange, Feeding, Note, Sleep, Timer,
class ChildSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Child
fields = ('first_name', 'last_name', 'birth_date')
fields = ('first_name', 'last_name', 'birth_date', 'slug')
lookup_field = 'slug'
class DiaperChangeSerializer(serializers.HyperlinkedModelSerializer):

View File

@ -15,9 +15,10 @@ from .serializers import (ChildSerializer, DiaperChangeSerializer,
class ChildViewSet(viewsets.ModelViewSet):
queryset = Child.objects.all()
serializer_class = ChildSerializer
lookup_field = 'slug'
def get_queryset(self):
params = ['first_name', 'last_name']
params = ['first_name', 'last_name', 'slug']
return filter_by_params(self.request, Child, params)

View File

@ -9,7 +9,7 @@ from .models import (Child, DiaperChange, Feeding, Note, Sleep, Timer,
@admin.register(Child)
class ChildAdmin(admin.ModelAdmin):
list_display = ('first_name', 'last_name', 'birth_date',)
list_display = ('first_name', 'last_name', 'birth_date', 'slug')
list_filter = ('last_name',)
search_fields = ('first_name', 'last_name', 'birth_date',)

View File

@ -2,6 +2,7 @@
from __future__ import unicode_literals
from django.db import models
from django.template.defaultfilters import slugify
from django.utils import timezone
from .utils import duration_string
@ -11,6 +12,7 @@ class Child(models.Model):
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
birth_date = models.DateField(blank=False, null=False)
slug = models.SlugField(max_length=100, unique=True, editable=False)
objects = models.Manager()
@ -22,6 +24,10 @@ class Child(models.Model):
def __str__(self):
return '{} {}'.format(self.first_name, self.last_name)
def save(self, *args, **kwargs):
self.slug = slugify(self)
super(Child, self).save(*args, **kwargs)
class DiaperChange(models.Model):
child = models.ForeignKey('Child', related_name='diaper_change')