Add filter support.

This commit is contained in:
Christopher Charbonneau Wells 2017-08-13 16:15:57 -04:00
parent 105adec971
commit eaabcc6faf
4 changed files with 60 additions and 1 deletions

View File

@ -5,3 +5,4 @@ url = "https://pypi.python.org/simple"
[packages] [packages]
django = "*" django = "*"
djangorestframework = "*" djangorestframework = "*"
django-filter = "*"

5
Pipfile.lock generated
View File

@ -1,7 +1,7 @@
{ {
"_meta": { "_meta": {
"hash": { "hash": {
"sha256": "cf364d603966857cd128557bc679634e0e6754c0ee8fe7d80af6b38d5e2dc745" "sha256": "a0ef885217179eed1565af7ec6a732e8b141fb42c156f152380a65feac7cb1fe"
}, },
"requires": {}, "requires": {},
"sources": [ "sources": [
@ -15,6 +15,9 @@
"django": { "django": {
"version": "==1.11.4" "version": "==1.11.4"
}, },
"django-filter": {
"version": "==1.0.4"
},
"djangorestframework": { "djangorestframework": {
"version": "==3.6.3" "version": "==3.6.3"
}, },

View File

@ -14,26 +14,76 @@ class BabyViewSet(viewsets.ModelViewSet):
queryset = Baby.objects.all() queryset = Baby.objects.all()
serializer_class = BabySerializer serializer_class = BabySerializer
def get_queryset(self):
queryset = Baby.objects.all()
for param in ['first_name', 'last_name']:
value = self.request.query_params.get(param, None)
if value is not None:
queryset = queryset.filter(**{param: value})
return queryset
class DiaperChangeViewSet(viewsets.ModelViewSet): class DiaperChangeViewSet(viewsets.ModelViewSet):
queryset = DiaperChange.objects.all() queryset = DiaperChange.objects.all()
serializer_class = DiaperChangeSerializer serializer_class = DiaperChangeSerializer
def get_queryset(self):
queryset = DiaperChange.objects.all()
for param in ['baby__last_name', 'wet', 'solid', 'color']:
value = self.request.query_params.get(param, None)
if value is not None:
queryset = queryset.filter(**{param: value})
return queryset
class FeedingViewSet(viewsets.ModelViewSet): class FeedingViewSet(viewsets.ModelViewSet):
queryset = Feeding.objects.all() queryset = Feeding.objects.all()
serializer_class = FeedingSerializer serializer_class = FeedingSerializer
def get_queryset(self):
queryset = Feeding.objects.all()
for param in ['baby__last_name', 'type', 'method']:
value = self.request.query_params.get(param, None)
if value is not None:
queryset = queryset.filter(**{param: value})
return queryset
class SleepViewSet(viewsets.ModelViewSet): class SleepViewSet(viewsets.ModelViewSet):
queryset = Sleep.objects.all() queryset = Sleep.objects.all()
serializer_class = SleepSerializer serializer_class = SleepSerializer
def get_queryset(self):
queryset = Sleep.objects.all()
for param in ['baby__last_name']:
value = self.request.query_params.get(param, None)
if value is not None:
queryset = queryset.filter(**{param: value})
return queryset
class TummyTimeViewSet(viewsets.ModelViewSet): class TummyTimeViewSet(viewsets.ModelViewSet):
queryset = TummyTime.objects.all() queryset = TummyTime.objects.all()
serializer_class = TummyTimeSerializer serializer_class = TummyTimeSerializer
def get_queryset(self):
queryset = TummyTime.objects.all()
for param in ['baby__last_name']:
value = self.request.query_params.get(param, None)
if value is not None:
queryset = queryset.filter(**{param: value})
return queryset
class UserViewSet(viewsets.ModelViewSet): class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all() queryset = User.objects.all()

View File

@ -9,28 +9,33 @@ from .models import Baby, DiaperChange, Feeding, Sleep, TummyTime
@admin.register(Baby) @admin.register(Baby)
class BabyAdmin(admin.ModelAdmin): class BabyAdmin(admin.ModelAdmin):
list_display = ('first_name', 'last_name', 'birth_date',) list_display = ('first_name', 'last_name', 'birth_date',)
list_filter = ('last_name',)
search_fields = ('first_name', 'last_name', 'birth_date',) search_fields = ('first_name', 'last_name', 'birth_date',)
@admin.register(DiaperChange) @admin.register(DiaperChange)
class DiaperChangeAdmin(admin.ModelAdmin): class DiaperChangeAdmin(admin.ModelAdmin):
list_display = ('baby', 'time', 'wet', 'solid', 'color') list_display = ('baby', 'time', 'wet', 'solid', 'color')
list_filter = ('baby', 'wet', 'solid', 'color')
search_fields = ('baby__first_name', 'baby__last_name',) search_fields = ('baby__first_name', 'baby__last_name',)
@admin.register(Feeding) @admin.register(Feeding)
class FeedingAdmin(admin.ModelAdmin): class FeedingAdmin(admin.ModelAdmin):
list_display = ('start', 'end', 'duration', 'baby', 'type', 'method',) list_display = ('start', 'end', 'duration', 'baby', 'type', 'method',)
list_filter = ('baby', 'type', 'method',)
search_fields = ('baby__first_name', 'baby__last_name', 'type', 'method',) search_fields = ('baby__first_name', 'baby__last_name', 'type', 'method',)
@admin.register(Sleep) @admin.register(Sleep)
class SleepAdmin(admin.ModelAdmin): class SleepAdmin(admin.ModelAdmin):
list_display = ('start', 'end', 'duration', 'baby',) list_display = ('start', 'end', 'duration', 'baby',)
list_filter = ('baby',)
search_fields = ('baby__first_name', 'baby__last_name',) search_fields = ('baby__first_name', 'baby__last_name',)
@admin.register(TummyTime) @admin.register(TummyTime)
class TummyTimeAdmin(admin.ModelAdmin): class TummyTimeAdmin(admin.ModelAdmin):
list_display = ('start', 'end', 'duration', 'baby', 'milestone',) list_display = ('start', 'end', 'duration', 'baby', 'milestone',)
list_filter = ('baby',)
search_fields = ('baby__first_name', 'baby__last_name', 'milestone',) search_fields = ('baby__first_name', 'baby__last_name', 'milestone',)