Added products model

This commit is contained in:
Keannu Christian Bernasol 2023-03-05 21:56:48 +08:00
parent 62237c9a3c
commit d98c88482d
14 changed files with 98 additions and 3 deletions

0
ivy/products/__init__.py Normal file
View file

3
ivy/products/admin.py Normal file
View file

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

6
ivy/products/apps.py Normal file
View file

@ -0,0 +1,6 @@
from django.apps import AppConfig
class ProductsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'products'

View file

@ -0,0 +1,24 @@
# Generated by Django 4.1.7 on 2023-03-05 13:54
from django.db import migrations, models
import django.utils.timezone
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Product',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=20)),
('quantity', models.IntegerField(default=0)),
('date_added', models.DateTimeField(default=django.utils.timezone.now, editable=False)),
],
),
]

View file

12
ivy/products/models.py Normal file
View file

@ -0,0 +1,12 @@
from django.db import models
from django.utils.timezone import now
# Create your models here.
class Product(models.Model):
name = models.CharField(max_length=20)
quantity = models.IntegerField(default=0)
date_added = models.DateTimeField(default=now, editable=False)
def __str__(self):
return self.title

View file

@ -0,0 +1,12 @@
from rest_framework import serializers
from .models import Product
class ProductSerializer(serializers.HyperlinkedModelSerializer):
date_added = serializers.DateTimeField(
format="%d-%m-%Y %I:%M%p", read_only=True)
class Meta:
model = Product
fields = ('name', 'quantity', 'date_added')
read_only_fields = ('id', 'date_added')

3
ivy/products/tests.py Normal file
View file

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

13
ivy/products/urls.py Normal file
View file

@ -0,0 +1,13 @@
from django.urls import include, path
from rest_framework import routers
from . import views
router = routers.DefaultRouter()
router.register(r'products', views.ProductViewSet)
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
path('', include(router.urls)),
]

10
ivy/products/views.py Normal file
View file

@ -0,0 +1,10 @@
from rest_framework.permissions import IsAuthenticated
from rest_framework import viewsets
from .serializers import ProductSerializer
from .models import Product
class ProductViewSet(viewsets.ModelViewSet):
permission_classes = [IsAuthenticated]
serializer_class = ProductSerializer
queryset = Product.objects.all()