From 31bbf85d66bb7b018a5b782fee01a654819a97bb Mon Sep 17 00:00:00 2001 From: badour Date: Thu, 9 Dec 2021 18:02:37 +0300 Subject: [PATCH] first_commit --- commerce/controllers.py | 26 ++++++++++++++++++++++++++ commerce/migrations/0001_initial.py | 2 +- commerce/models.py | 2 ++ commerce/tests.py | 1 + config/urls.py | 12 ++++++++++++ manage.py | 1 + 6 files changed, 43 insertions(+), 1 deletion(-) diff --git a/commerce/controllers.py b/commerce/controllers.py index 91ea44a..0dc6353 100644 --- a/commerce/controllers.py +++ b/commerce/controllers.py @@ -1,3 +1,29 @@ +from django.db import router +from django.http.response import JsonResponse from django.shortcuts import render +from ninja.router import Router +from commerce.models import Address, Product +from ninja import NinjaAPI +from django.forms.models import model_to_dict +from django.http import JsonResponse + + + # Create your views here. +api= NinjaAPI() + +product_c= Router() +address_c = Router() + +@product_c.get('/') +def all_product(request): + #return JsonResponse({"product", list(Product.objects.all())}) + data= Product.objects.all().values() + return JsonResponse({"product": list(data)}) + +@address_c.get('/address') +def all_add(request): + ddata= Address.objects.all().values() + return JsonResponse({"address": list(ddata)}) + diff --git a/commerce/migrations/0001_initial.py b/commerce/migrations/0001_initial.py index 8e57e0f..cb637b8 100644 --- a/commerce/migrations/0001_initial.py +++ b/commerce/migrations/0001_initial.py @@ -8,7 +8,7 @@ class Migration(migrations.Migration): - initial = True + initial = True # means first migration dependencies = [ migrations.swappable_dependency(settings.AUTH_USER_MODEL), diff --git a/commerce/models.py b/commerce/models.py index dd7de89..463f801 100644 --- a/commerce/models.py +++ b/commerce/models.py @@ -210,3 +210,5 @@ class Address(Entity): def __str__(self): return f'{self.user.first_name} - {self.address1} - {self.address2} - {self.phone}' + + diff --git a/commerce/tests.py b/commerce/tests.py index 7ce503c..6d86c9f 100644 --- a/commerce/tests.py +++ b/commerce/tests.py @@ -1,3 +1,4 @@ from django.test import TestCase +from models import Product # Create your tests here. diff --git a/config/urls.py b/config/urls.py index d52e577..0763e92 100644 --- a/config/urls.py +++ b/config/urls.py @@ -15,7 +15,19 @@ """ from django.contrib import admin from django.urls import path +from django.urls import path, include +from ninja import NinjaAPI +from commerce.controllers import product_c, address_c +import ninja + +api= NinjaAPI() + +api.add_router('/', product_c) +api.add_router('/', address_c) urlpatterns = [ + path('admin/', admin.site.urls), + path('api/', api.urls) + ] diff --git a/manage.py b/manage.py index 8e7ac79..9a73933 100755 --- a/manage.py +++ b/manage.py @@ -4,6 +4,7 @@ import sys + def main(): """Run administrative tasks.""" os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')