Install python (pyenv or asdf)
git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.9.0
echo ". $HOME/.asdf/asdf.sh" >> ~/.bashrc
echo ". $HOME/.asdf/completions/asdf.bash" >> ~/.bashrc
asdf info
asdf plugin-add python
asdf install python 3.10.3
asdf global python 3.10.3
# Restart your shell
python -V
# Python 3.10.2
which python
# /Users/your-user/.asdf/shims/python
Install Poetry
curl -sSL https://install.python-poetry.org | python -
poetry --version
Create the project
poetry new bookshelf # Bookshelf is the name of the project
cd bookshelf
rm -r bookshelf tests # Django will generate those for us
poetry shell
which python
poetry add Django # Note the capital D
django-admin startproject bookshelf .
Initialise the project
python manage.py runserver
python manage.py migrate
python manage.py createsuperuser
Open the website and the admin page http://localhost:8000/ http://localhost:8000/admin/
Initialise the books
app
python manage.py startapp books
Create the models
from django.db import models
class Book(models.Model):
name = models.CharField(max_length=150)
year_of_publishing = models.PositiveSmallIntegerField()
class Author(models.Model):
name = models.CharField(max_length=150)
python manage.py makemigrations books --dry-run
python manage.py makemigrations books --name add_initial_models
Connect Book and Author
author = models.ForeignKey('Author', on_delete=models.CASCADE)
python manage.py makemigrations books --name add_authors_to_book
python manage.py migrate
Add the admin site
@admin.register(Book)
class BookAdmin(admin.ModelAdmin):
list_display = ("id", "__str__", "author")
@admin.register(Author)
class AuthorAdmin(admin.ModelAdmin):
list_display = ("id", "name",)
Install DRF
poetry add djangorestframework markdown django-filter
# bookshelf/settings.py
INSTALLED_APPS = [
...
'rest_framework',
]
# bookshelf/urls.py
urlpatterns = [
# ...
path('api-auth/', include('rest_framework.urls'))
]
Add serialisers
Add view sets
Add router and url
#montel #mandatum