rest_api set,

This commit is contained in:
Orabis 2024-10-16 04:10:32 +02:00
parent f7826b56bd
commit bc7ce8106e
5 changed files with 40 additions and 22 deletions

View File

@ -9,7 +9,7 @@ class Product(models.Model):
modification_date = models.DateTimeField(auto_now=True)
def __str__(self):
return self.nom
return self.name
class Alerte(models.Model):
@ -18,6 +18,3 @@ class Alerte(models.Model):
message = models.CharField(max_length=200, null=True, blank=True)
creation_date = models.DateTimeField(auto_now_add=True)
modification_date = models.DateTimeField(auto_now=True)
def __str__(self):
return f"Alerte pour {self.product.name} - Seuil: {self.stock_limit}"

View File

@ -0,0 +1,17 @@
from rest_framework import serializers
from .models import *
class ProductSerializer(serializers.ModelSerializer):
class Meta:
model = Product
fields = ["id", "name", "description", "quantity", "creation_date", "modification_date"]
class AlerteSerializer(serializers.ModelSerializer):
product = ProductSerializer
class Meta:
model = Alerte
fields = ["id", "product", "stock_limit", "message", "creation_date", "modification_date"]

View File

@ -103,6 +103,13 @@ AUTH_PASSWORD_VALIDATORS = [
},
]
REST_FRAMEWORK = {
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
]
}
# Internationalization
# https://docs.djangoproject.com/en/5.1/topics/i18n/

View File

@ -1,22 +1,12 @@
"""
URL configuration for StockSeeker project.
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/5.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.urls import include, path
from rest_framework import routers
from . import views
router = routers.DefaultRouter()
router.register(r'product', views.ProductViewSet)
urlpatterns = [
path('', include(router.urls)),
path('admin/', admin.site.urls),
]

View File

@ -1 +1,8 @@
# Create your views here.
from rest_framework import viewsets
from .models import *
from .serializers import *
class ProductViewSet(viewsets.ModelViewSet):
queryset = Product.objects.all()
serializer_class = ProductSerializer