(https://gist.github.com/seanbehan/168a277a5a23086e76000a59e54fb3d5)
- Create
manage.py
andsettings.py
in the root directory. Then install the dependencies in your environment.pip install flask django dj-database-url psycopg2
# manage.py import os import sys import dotenv dotenv.load_dotenv() if __name__ == "__main__": from django.core.management import execute_from_command_line execute_from_command_line(sys.argv)
# settings.py import os import dj_database_url DATABASES = { 'default': dj_database_url.config(default=os.environ['DATABASE_URL']) } INSTALLED_APPS = ( 'instagram', ) SECRET_KEY = os.environ['SECRET_KEY']
# .env DJANGO_SETTINGS_MODULE=settings DATABASE_URL="postgres://localhost:5432/<db_name>" SECRET_KEY=""
- Create a folder with the name state in your
INSTALLED_APPS
and create the file in the folder as below.django-nextagram/ instagram_api/ instagram_web/ instagram/ migrations/ __init__.py # empty file models/ __init__.py # import models base_model.py user.py .env app.py config.py manage.py settings.py ...
- Add the code below in
app.py
to load the apps with configuration.# app.py from django.apps import apps from django.conf import settings apps.populate(settings.INSTALLED_APPS)
-
Indexes (https://docs.djangoproject.com/en/3.0/ref/models/options/#django.db.models.Options.indexes)
class Meta: indexes = [ models.Index(fields=['username', 'email']), ]
-
Model field reference https://docs.djangoproject.com/en/dev/ref/models/fields/
-
Differentiate null=True and blank=True https://stackoverflow.com/questions/8609192/differentiate-null-true-blank-true-in-django
- Creating new migrations based on the changes you have made to your models
python manage.py makemigrations
- Applying and unapplying migrations
python manage.py migrate
- lists all migrations and their status
python manage.py showmigrations
- Reversing migrations
python manage.py migrate <installed_app_name> <migration_file>
QuerySet API reference : (https://docs.djangoproject.com/en/3.0/ref/models/querysets/) Read more here : (https://books.agiliq.com/projects/django-orm-cookbook/en/latest/)
- Create
User(username="helloworld").save() User.objects.create(username="helloworld")
- Read
backref => related_name
# single object user = User.objects.get(username="helloworld") # multiple object users = User.objects.all() # select all users users = User.objects.filter() # select all users images = Image.objects.filter(user=1, id=1) # select with filter images = Image.objects.filter(user=1).exclude(id=1) # select with filter and exclude
join => select_related# image.py class Image(BaseModel): user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="images") # flask shell for image in User.objects.get(id=1).images.all(): print(image.id)
donation = Donation.objects.select_related('image__user').get(id=1) image = donation.image # Doesn't hit the database. user = image.user # Doesn't hit the database.
- Update
user = User.objects.get(id=1) user.username = "new_name" user.save() # update multiple at once Image.objects.filter(user=1).update(user=2) # return number of rows updated
- Delete
# delete single object User.objects.get(id=1).delete() # delete multiple at once Image.objects.filter(user=1).delete()
- Ways to get SQL query
print(User.filter().query) User.filter().query.sql_with_params()
Add the environment variable below to disable the collectstatic during a deploy.
# .env
DISABLE_COLLECTSTATIC=1