Skip to content

Instantly share code, notes, and snippets.

@NimishVerma
Last active January 10, 2023 09:06
Show Gist options
  • Save NimishVerma/6489d6741bada2ec015433e822bf9bc6 to your computer and use it in GitHub Desktop.
Save NimishVerma/6489d6741bada2ec015433e822bf9bc6 to your computer and use it in GitHub Desktop.
# Instatiate Django and import settings
import os
#mark django settings module as settings.py
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
#instantiate a web sv for django which is a wsgi
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
#import your models schema
from MyApp.models import Person
#Create Operations here
data_dict={'name':'Nimish','age':23,email:'nimishverma@ymail.com'}
person = Person(**data_dict)
person.save()
# Read operation logic
person = Person.objects.get()
print(f'Hello, I am {person.name}, {person.age} y/o. Reachable at {person.email}')
#Prints Hello, I am Nimish, 23 y/o. Reachable at nimishverma@ymail.com
import os, sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
# MyApp/models.py
from django.db import models
class Person(models.Model):
name = models.TextField()
age = models.IntegerField(min_value=18, max_value=110)
email = models.EmailField()
import os
# This defines the base dir for all relative imports for our project, put the file in your root folder so the
# base_dir points to the root folder
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
# According to your data file, you can change the engine, like mysql, postgresql, mongodb etc make sure your data is
# directly placed in the same folder as this file, if it is not, please direct the 'NAME' field to its actual path.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
#Since we only have one app which we use
INSTALLED_APPS = (
'MyApp',
)
# Write a random secret key here
SECRET_KEY = '4e&6aw+(5&cg^_!05r(&7_#dghg_pdgopq(yk)xa^bog7j)^*j'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment