I've used Django ORM for one of my web-app and I'm very much comfortable with it. Now I've a new requirement which needs database but nothing else that Django offers. I don't want to invest more time in learning one more ORM like sqlalchemy.
I think I can still do from django.db import models
and create models, but then without manage.py
how would I do migration and syncing?
Django doesn't have a separate package for it's ORM, but it's still possible to use it on its own.
The Django web framework includes a default object-relational mapping layer (ORM) that can be used to interact with application data from various relational databases such as SQLite, PostgreSQL and MySQL. The Django ORM is an implementation of the object-relational mapping (ORM) concept.
Yes that is possible, but a lot of ways how Django can help with webdevelopment are based on its models. For example based on a model Django can make a ModelForm [Django-doc] to automate rendering HTML forms that map to the model, validating user input, and saving it to the database.
We can access the ORM by running python manage.py shell from the main directory of our Django project. Type "help", "copyright", "credits" or "license" for more information. This will bring us into an interactive console.
There are a lot of answers out there that work with older versions of Django, but Django is constantly updating and in my research I found no viable answer for Django 1.8/1.9, so I had to roll my own. Here's how you do it:
Project Structure:
├── data │ ├── __init__.py │ ├── migrations │ │ └── __init__.py │ └── models.py ├── main.py ├── manage.py └── settings.py
The data
directory and migrations directory contain empty __init__.py
files. The sample models.py
file reads as follows:
# models.py from django.db import models class User(models.Model): name = models.CharField(max_length=255) email = models.EmailField(max_length=255)
The manage.py
file is the typical Django manage.py
file. Just be sure to change the settings param in os.environ.setdefault
if you copy it from a fresh django-admin startproject
command:
#!/usr/bin/env python # manage.py import os import 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)
The settings.py
file requires 3 settings: DATABASES, INSTALLED_APPS, and SECRET_KEY. Refer to Django docs for DBs that aren't SQLite:
# settings.py DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': 'sqlite.db', } } INSTALLED_APPS = ( 'data', ) SECRET_KEY = 'REPLACE_ME'
The real trick is in main.py
, which will be where you can code against your models. Apparently you have to use wsgi
to get things working with these two lines:
from django.core.wsgi import get_wsgi_application application = get_wsgi_application()
Here's a sample main.py:
# main.py # Django specific settings import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings") ### Have to do this for it to work in 1.9.x! from django.core.wsgi import get_wsgi_application application = get_wsgi_application() ############# # Your application specific imports from data.models import * #Add user user = User(name="someone", email="[email protected]") user.save() # Application logic first_user = User.objects.all()[0] print(first_user.name) print(first_user.email)
This project along with this post were helpful starting points for me to find the answer, and my pull request with working code for Django 1.9 was merged, so you can grab the code from masnun's repo now. If you know of a better way, please submit a pull request.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With