Skip to content

FR98/simple-django-db-manager

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 

Repository files navigation

simple-django-db-manager

Author: Francisco Rosal

Simple db manager for Django projects

Tips for start your project

  • Install Python
  • Install Postgres
  • Install Python Environment
    Linux:
    $ sudo apt install python3-env
    MacOS:
    $ sudo pip3 install virtualenv
  • Install Django
    $ pip3 install Django
  • Create project
    $ django-admin startproject your-django-project
  • Create repo
    $ git init
    $ git add .
    $ git commit -m "feat: first commit"
    $ git branch -M master
    $ git remote add origin [email protected]:org/repo.git
    $ git push -u origin master
    On github.com:
        Add README
        Add LICENSE
        Add .gitignore
    $ git pull origin master
  • Create and activate python env
    Linux:
    $ python3 -m venv venv
    $ source venv/bin/activate
    MacOS:
    $ virtualenv venv
    $ source venv/bin/activate
  • Install Django
    $ pip3 install Django
  • Install psycopg2
    • Install and test installation
    Linux:
    $ pip install psycopg2
    $ python -c "import psycopg2" --verbose
    MacOS:
    $ pip3 install psycopg2-binary
    $ python3 -c "import psycopg2" --verbose
  • Install ipython
    $ pip3 install ipython
  • Configure database
  • Run Server
    $ python3 manage.py runserver
  • Create an app
    $ python3 manage.py startapp myapp
  • Open shell
    $ python3 manage.py shell

Configure Database

  • Place load_data.py inside your django project

    • Tip: next to manage.py
        your-django-project/
            manage.py
            credentials.py
            load_data.py
  • Modify /your-django-project/credentials.py

    • Read instructions inside credentials.py
  • Add this lines on /your-django-project/settings.py

    • Add
    import credentials
    • Replace default in DATABASES
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': credentials.DEVELOPMENT_DATABASE['NAME'],
        'USER': credentials.DEVELOPMENT_DATABASE['USER'],
        'PASSWORD': credentials.DEVELOPMENT_DATABASE['PASSWORD'],
        'HOST': credentials.DEVELOPMENT_DATABASE['HOST'],
        'PORT': credentials.DEVELOPMENT_DATABASE['PORT'],
    }
    • Recommendation: Add too...
    LOGIN_URL = "/"
    LOGIN_REDIRECT_URL = "home"
    LOGOUT_REDIRECT_URL = "/"
  • To reset db and load data run

    $ python3 load_data.py

You would like to know...

  • Requirements
    $ pip3 freeze >> requirements.txt
    $ pip3 install -r requirements.txt
  • CLI Postgres
    $ psql -h localhost -U postgres -W

Make an API

  • To do list:
    • Apps

    • Models

    • Django RestFramework

      $ pip3 install djangorestframework
      $ pip3 install django-filter

      settings.py

      INSTALLED_APPS = [
          ...
          'rest_framework',
      ]
      
      REST_FRAMEWORK = {
          'DEFAULT_PERMISSION_CLASSES': [
              'rest_framework.permissions.AllowAny',
          ],
      }

      urls.py

      from django.contrib import admin
      from django.conf.urls import url, include
      
      urlpatterns = [
          url('admin/', admin.site.urls),
          url(r'^api-auth/', include('rest_framework.urls', namespace = 'rest_framework')),
      ]
    • Serializers

    • ViewSets

    • Routers and Urls

      urls.py

      from rest_framework import routers
      
      from users.views import UserViewSet
      
      router = routers.DefaultRouter()
      router.register(r'users', UserViewSet)
      
      urlpatterns = [
          ...
          url(r'^api/', include(router.urls)),
      ]
    • Custom Actions

    • JWT Authentication and Authorization

      $ pip3 install djangorestframework-jwt

      settings.py

      REST_FRAMEWORK = {
          'DEFAULT_PERMISSION_CLASSES': [
              # 'rest_framework.permissions.AllowAny',
              'rest_framework.permissions.IsAuthenticated',
          ],
          'DEFAULT_AUTHENTICATION_CLASSES': (
              'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
              # 'rest_framework.authentication.SessionAuthentication',
              # 'rest_framework.authentication.BasicAuthentication',
          ),
      }
      
      JWT_AUTH = {
          'JWT_ALLOW_REFRESH': True,
          'JWT_EXPIRATION_DELTA': datetime.timedelta(seconds=3600),
      }

      urls.py

      from rest_framework_jwt.views import (
          obtain_jwt_token,
          refresh_jwt_token,
          verify_jwt_token,
      )
      
      urlpatterns = [
          ...
          url(r'^api-token/auth/', obtain_jwt_token),
          url(r'^api-token/refresh/', refresh_jwt_token),
          url(r'^api-token/verify/', verify_jwt_token),
      ]
    • Object Level Permissions

      $ pip3 install django-guardian

      settings.py

      INSTALLED_APPS = [
          ...
          'rest_framework',
          'guardian',
      ]
      
      AUTHENTICATION_BACKENDS = (
          'django.contrib.auth.backends.ModelBackend',
          'guardian.backends.ObjectPermissionBackend'
      )

      views.py

      from django.contrib.auth.models import User
      from users.permissions import APIPermissionClassFactory
      
      class UserViewSet(viewsets.ModelViewSet):
      queryset = User.objects.all()
      serializer_class = UserSerializer
      permission_classes = (
          APIPermissionClassFactory(
              name='UserPermission',
              permission_configuration={
                  'base': {
                      'create': True,
                      'list': True,
                  },
                  'instance': {
                      'retrieve': True,
                      'update': True,
                      'partial_update': True,
                      'destroy': True,
                  }
              }
          ),
      )

Use React-Redux with your django project!

  • Create React App

    $ npx create-react-app your-react
    $ yarn add react-redux
    $ yarn add redux
    $ yarn add redux-saga
    $ yarn add react-router-dom
    $ yarn add redux-persist
    $ yarn add jwt-decoded
    $ yarn add normalizr
  • Usefull to know

    $ yarn add uuid
    $ yarn add lodash
    $ yarn add express
  • To do list:

    • State
    • Types and Actions Creators
    • Reducers
    • Global Reducer
    • Selectors
    • Global Selector
    • Dummy Components
    • Smart Components

About

Simple db manager for Django projects

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages