From 2a78fa82629ca9b4e24e31688be24469b9b87f6c Mon Sep 17 00:00:00 2001 From: Matteo Parrucci Date: Tue, 3 Jan 2023 11:50:47 +0100 Subject: [PATCH] first working --- Readme.md | 2 +- mysuitablephone/urls.py | 2 +- suitablephones/models.py | 12 ++++++++++-- suitablephones/serializers.py | 10 ++++++++++ suitablephones/urls.py | 11 +++++++++-- suitablephones/views.py | 13 ++++++++++--- 6 files changed, 41 insertions(+), 9 deletions(-) create mode 100644 suitablephones/serializers.py diff --git a/Readme.md b/Readme.md index 7b1a788..91a1270 100644 --- a/Readme.md +++ b/Readme.md @@ -24,4 +24,4 @@ virtualenv . bin/pip install -r requirements.txt bin/python manage.py migrate bin/python manage.py createsuperuser -bin/python manage.py startserver +bin/python manage.py runserver diff --git a/mysuitablephone/urls.py b/mysuitablephone/urls.py index 20929b9..584b9e3 100644 --- a/mysuitablephone/urls.py +++ b/mysuitablephone/urls.py @@ -17,7 +17,7 @@ from django.contrib import admin from django.urls import include, path urlpatterns = [ - path('api-auth/', include('rest_framework.urls')), + path('', include('suitablephones.urls')), path('admin/', admin.site.urls), ] diff --git a/suitablephones/models.py b/suitablephones/models.py index 8db3650..4146567 100644 --- a/suitablephones/models.py +++ b/suitablephones/models.py @@ -7,6 +7,9 @@ class Bluetooth(models.Model): spec = models.CharField(max_length=100) profile = models.CharField(max_length=100) + def __str__(self): + return f"Bluetooth: {self.spec}" + class Camera(models.Model): class FLASHES(models.TextChoices): @@ -15,6 +18,9 @@ class Camera(models.Model): megapixel = models.DecimalField(max_digits=5, decimal_places=1) flash = models.CharField(max_length=100, choices=FLASHES.choices) + + def __str__(self): + return f"Camera: {self.megapixel} - {self.flash}" #Devices.object.filter(flash=Camera.FLASEHS.LED) @@ -53,5 +59,7 @@ class Device(models.Model): vendor = models.CharField(max_length=100) vendor_short = models.CharField(max_length=100) #versions = - - wifi = models.CharField(max_length=100) \ No newline at end of file + wifi = models.CharField(max_length=100) + + def __str__(self): + return 'Device: ' + self.name \ No newline at end of file diff --git a/suitablephones/serializers.py b/suitablephones/serializers.py new file mode 100644 index 0000000..cc10c8a --- /dev/null +++ b/suitablephones/serializers.py @@ -0,0 +1,10 @@ +from rest_framework import serializers + +from suitablephones.models import Device + + +# Serializers define the API representation. +class DeviceSerializer(serializers.HyperlinkedModelSerializer): + class Meta: + model = Device + fields = ['cpu', 'kernel', 'name'] \ No newline at end of file diff --git a/suitablephones/urls.py b/suitablephones/urls.py index bc8668f..f4b7629 100644 --- a/suitablephones/urls.py +++ b/suitablephones/urls.py @@ -1,7 +1,14 @@ -from django.urls import path -from suitablephones.views import SuitablePhones +from django.urls import include, path +from rest_framework import routers + +from suitablephones.views import DeviceViewSet, SuitablePhones + +router = routers.DefaultRouter() +router.register(r'devices', DeviceViewSet) urlpatterns = [ + path('api/', include(router.urls)), + path('api-auth/', include('rest_framework.urls', namespace='rest_framework')), path('suitablephones/', SuitablePhones.as_view(), name='suitablephones'), ] diff --git a/suitablephones/views.py b/suitablephones/views.py index 69d3c30..2ce64f7 100644 --- a/suitablephones/views.py +++ b/suitablephones/views.py @@ -1,7 +1,8 @@ -from django.shortcuts import render -from django.http import HttpResponse from django.views.generic.list import ListView -from suitablephones.models import Device, Camera +from rest_framework import viewsets + +from suitablephones.models import Device +from suitablephones.serializers import DeviceSerializer class SuitablePhones(ListView): @@ -14,3 +15,9 @@ class SuitablePhones(ListView): def get_queryset(self): camera = self.get_request_camera() return self.model.objects.filter(camera=camera) + + +# ViewSets define the view behavior. +class DeviceViewSet(viewsets.ModelViewSet): + queryset = Device.objects.all() + serializer_class = DeviceSerializer \ No newline at end of file