Django Ninja is a web framework for building APIs with Django and Python 3.6+ type hints.
Key features:
pip install django-ninja
(If you already have an existing Django project, skip to the next step).
Start a new Django project (or use an existing one).
We are going to create the Project by the name of sample_api, our project will provide API for creating contracts and Retrieving contacts.
django-admin startproject sample_api
First steps
Let's create An App for all main operations in our Sample_api project, by the name of core
django-admin startapp core
Then our core app will have the following files: __init__.py admin.py models.py
tests.py apps.py and views.py
now we need to add models in the models.py file, but let's get to know some details about Django Models because Django Ninja uses Models from Django Framework.
A model is the single, definitive source of information about your data. It contains the essential fields and behaviors of the data you’re storing. Generally, each model maps to a single database table.
The basics:
django.db.models.Model
.During model creation we use Fields, let's get to know about Models Fields
The most important part of a model – and the only required part of a model – is the list of database fields it defines. Fields are specified by class attributes. Be careful not to choose field names that conflict with the models API like clean
, save
, or delete
.
Each field in your model should be an instance of the appropriate Field
class. Django uses the field class types to determine a few things:
INTEGER
, VARCHAR
, TEXT
).<input type="text">
, <select>
).Django ships with dozens of built-in field types; you can find the complete list in the model field reference. You can easily write your own fields if Django’s built-in ones don’t do the trick; see How to create custom model fields.
Each field takes a certain set of field-specific arguments (documented in the model field reference). For example, CharField
(and its subclasses) require a max_length
the argument which specifies the size of the VARCHAR
database field used to store the data.
There’s also a set of common arguments available to all field types. All are optional. They’re fully explained in the reference, but here’s a quick summary of the most often-used ones:
If True
, Django will store empty values as NULL
in the database. Default is False
.
blank
True
, the field is allowed to be blank. Default is False
.null
. null
is purely database-related, whereas blank
is validation-related. If a field has blank=True
, form validation will allow entry of an empty value. If a field has blank=False
, the field will be required.Once we have defined your models, we need to tell Django we’re going to use those models. we do this by editing our settings file and changing the INSTALLED_APPS
setting to add the name of the module that contains our models.py
.
For example, if the models for your application live in the module myapp.models
(the package structure that is created for an application by the manage.py startapp
script), INSTALLED_APPS
should read, in part:
INSTALLED_APPS = [ #... 'core', #... ]
When you add new apps to INSTALLED_APPS
, be sure to run manage.py migrate
, optionally making migrations for them first with manage.py makemigrations
.
Now we have the Database model by the name of Contact, then after creating the model we need to make migrations and migrating so as to create a database table and persist the changes of our project.
python manage.py makemigrations
Then we need to migrate:
python manage.py migrate
Second Step we need to create Schema based on our Models.py, but let's get to know about Schema
Schemas from Django models
Schemas are very useful to define our validation rules and responses, but sometimes we need to reflect your database models into schemas and keep changes in sync.
ModelSchema
is a special base class that can automatically generate schemas from models.
All we need is to set model
and model_fields
attributes on our schema Config
To use all fields from a model - we can pass __all__
to model_field
To use all fields except a few, we can use model_exclude
configuration
To change default annotation for some field, or to add a new field, we just use annotated attributes as usual.
Now let's create our Schema.
core/schema.py
After creating our schema now let's create our views.py, but before we create it lets get some introduction about Django views
A view function, or view for short, is a Python function that takes a web request and returns a web response. This response can be the HTML contents of a web page, or a redirect, or a 404 error, or an XML document, or an image . . . or anything, really. The view itself contains whatever arbitrary logic is necessary to return that response. This code can live anywhere we want, as long as it’s on our Python path. There’s no other requirement–no “magic,” so to speak. For the sake of putting the code somewhere, the convention is to put views in a file called views.py
, placed in your project or application directory.
In our project we are going to use class-based views, so let's get some intro about class-based views.
Class-based views provide an alternative way to implement views as Python objects instead of functions. They do not replace function-based views, but have certain differences and advantages when compared to function-based views:
GET
, POST
, etc.) can be addressed by separate methods instead of conditional branching.At its core, a class-based view allows us to respond to different HTTP request methods with different class instance methods, instead of with conditionally branching code inside a single view function.
our views - core/views.py:
Now we have our views the next step is to create, APIs based on created views so as to create and retrieve contacts to and from the database.
To create API from our core app we need to use Routers.
Real-world applications can almost never fit all logic into a single file.
Django Ninja comes with an easy way to split your API into multiple modules using Routers.
To add API's to each of the Django applications, we create a api.py
module in each app
There are also times when you need to split your logic up even more. Django Ninja makes it possible to include a router into another router as many times as you like, and finally include the top-level router into the main API instance.
"Operation" can be one of the HTTP "methods":
Django Ninja comes with a decorator for each method:
@api.get("/path")
def get_operation(request):
...
@api.post("/path")
def post_operation(request):
...
@api.put("/path")
def put_operation(request):
...
@api.delete("/path")
def delete_operation(request):
...
@api.patch("/path")
def patch_operation(request):
...
If you need to handle multiple methods with a single function for a given path, you can use the api_operation
method:
@api.api_operation(["POST", "PATCH"], "/path")
def mixed(request):
...
This feature can also be used to implement other HTTP methods that don't have corresponding django-ninja methods, such as HEAD
or OPTIONS
.
@api.api_operation(["HEAD", "OPTIONS"], "/path")
def mixed(request):
...
let's see our core/api.py:
now we have our core app APIs, the thing that remains is to add our APIs to the main project sample_api/urls.py:
After all of these processes, now we need to start our project so as to view our APIs for testing.
to start the project we need to run the following command:
python manage.py runserver
to view APIs, we need to go to the browser and write http://127.0.0.1:8000/api/docs
by adding /docs this will provide us Swagger UI which is user friendly for API testing
Thanks,
Any suggestions, questions leave in the comment area below or email me via mail@ismohamedi.dev
The whole project source code is available on Github Sample APIs Github and LIVE view Sample APIs LIVE
Группа объявления в Иркутске в телеграмм. Размещение частных объявлений бесплатно! Коммерческие и рекламные объявления, согласно правил группы. #Иркутск #ОбъявленияИркутск #БесплатныеОбъявления #объявление #доскаобъявлений #барахолка #телеграм #телеграмм #telegram Присоединяйся, чтобы быть в курсе... объявление в Иркутске куплю Чаты остальных можно увидеть здесь!! бесплатные объявления
https://justpaste.it/b22ns
<a href="https://blyadsk.ru/">интим досуг в иркутске</a>
<a href="https://sex-138.ru/">sex-138.ru</a>
<a href="https://dosuchki1.ru">проститутки индивидуалки иркутск</a>
<a href="https://love74.ru/">снять проститутку</a>
<a href="https://sosamba-spb1.ru/sadovaya">проститутки садовая</a>
http://sniperprojects.freehostia.com/e107_plugins/forum/forum_viewtopic.php?735003.last
https://post.news/@/fishingtri
https://avenue18.ru/
https://1x-bet-india.com
It is interesting. Tell to me, please - where I can read about it?
https://www.shtfsocial.com/rentcarfycom
<a href="https://wedding-como.com/">оператор на свадьбу комо италия</a>
https://virtual-local-numbers.com/countries/21-mexico.html
123456
123456
123456
123456
123456
123456
123456
123456
123456
123456
123456
123456
123456
123456
123456
123456
123456
123456
123456
123456
123456
123456
123456
123456
123456'/**/and/**/DBMS_PIPE.RECEIVE_MESSAGE('o',2)='o
123456'/**/and/**/DBMS_PIPE.RECEIVE_MESSAGE('d',0)='d
123456
123456/**/and/**/4=DBMS_PIPE.RECEIVE_MESSAGE('p',2)
123456
123456/**/and/**/3=DBMS_PIPE.RECEIVE_MESSAGE('g',0)
123456
123456'and(select+1)>0waitfor/**/delay'0:0:2
123456
123456'and(select+1)>0waitfor/**/delay'0:0:0
123456
123456/**/and(select+1)>0waitfor/**/delay'0:0:2'/**/
123456
123456/**/and(select+1)>0waitfor/**/delay'0:0:0'/**/
123456
123456'/**/and(select'1'from/**/pg_sleep(2))::text>'0
123456
123456'/**/and(select'1'from/**/pg_sleep(0))::text>'0
123456
123456/**/and(select+1/**/from/**/pg_sleep(2))>0/**/
123456/**/and(select+1/**/from/**/pg_sleep(0))>0/**/
123456"and(select*from(select+sleep(2))a/**/union/**/select+1)="
123456"and(select*from(select+sleep(0))a/**/union/**/select+1)="
123456'and(select*from(select+sleep(2))a/**/union/**/select+1)='
123456'and(select*from(select+sleep(0))a/**/union/**/select+1)='
<%- 900434287+991209460 %>
(select*from(select+sleep(2)union/**/select+1)a)
123456
(select*from(select+sleep(0)union/**/select+1)a)
#set($c=863851269+869585257)${c}$c
123456
123456
${(846736249+822183643)?c}
123456'"\(
123456
123456
${890890727+801776975}
123456鎈'"\(
123456
123456
/*1*/{{830739370+962934228}}
123456'and/**/convert(int,sys.fn_sqlvarbasetostr(HashBytes('MD5','1269860965')))>'0
123456
123456"and"p"="v
123456
convert(int,sys.fn_sqlvarbasetostr(HashBytes('MD5','1487053833')))
123456
123456"and"o"="o
123456/**/and/**/cast(md5('1067385058')as/**/int)>0
123456
123456'and'q'='g
123456'and(select'1'from/**/cast(md5(1138473984)as/**/int))>'0
123456
123456'and'f'='f
extractvalue(1,concat(char(126),md5(1608016284)))
123456
123456/**/and+3=6
'-var_dump(md5(238235419))-'
123456"and/**/extractvalue(1,concat(char(126),md5(1648588234)))and"
123456/**/and+0=0
${@var_dump(md5(317549794))};
expr 932967672 + 920517160
123456
123456'and/**/extractvalue(1,concat(char(126),md5(1019785648)))and'
123456
123456
123456
123456&set /A 986291493+935994087
123456
123456
123456
123456
123456$(expr 941570917 + 936289680)
123456
123456
${960482659+944430052}
123456
123456|expr 874558418 + 909072495
123456
123456 expr 881334811 + 922593597
123456
https://www.mainmovs.com/
This is nice. Although it’s the first time I’m reading about django ninja, it’s definitely worth a try
Good material and good approach to follow
Thats great!!
Nice article.
Great content, Very usefully.