Skip to main content

Use Django Models in any Python Web Framework

Project description

# DJ Models

Use the Django ORM in any Python web framework.

## Install

pip3 install djmodels


## Getting Started

Create an app directory for your models and settings (DB connection details).

mkdir app
touch app/{__init__.py,models.py,settings.py}


Add your database settings to the settings module. See Django's docs on this for more info.

```python
SECRET_KEY = '<ACTUAL SECRET KEY>'
DATABASES = {
'default': {
'ENGINE': 'djmodels.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'mysecretpassword',
'HOST': '0.0.0.0',
'PORT': '5432',
}
}

INSTALLED_APPS = ['app']

```


Add a model to `app/models.py`

```python
from djmodels.db import models

class Person(models.Model):
name = models.CharField()
age = models.PositiveIntegerField()

```
Export your settings module

export DJMODELS_SETTINGS_MODULE=app.settings


Create migrations

$ djmodels-admin makemigrations app
# Migrations for 'app':
# - Create model Person


Run Migrate

$ djmodels-admin migrate app
# Operations to perform:
# Apply all migrations: app
# Running migrations:
# Applying app.0001_initial... OK


Import the model into any webframework and make queries. For example, Flask.

```python

from flask import Flask
from app.models import Person
app = Flask(__name__)

@app.route("/person/")
def get_random_person():
person = Person.objects.order_by('?').first()
return '{}'.format(person.name)

```

## Gotchas

- Make sure to set DJMODELS_SETTINGS_MODULE


## LICENSE
MIT


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

DJModels-0.0.4-py3-none-any.whl (3.4 MB view details)

Uploaded Python 3

File details

Details for the file DJModels-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: DJModels-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 3.4 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for DJModels-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 04195e4722d7d4ec0a96a814d61ffcea30fea387be18351adf5e69bcfa6b86f1
MD5 2afb5eb1197f740ed8d18499f3213827
BLAKE2b-256 922ab8056a270b72d8a64c90a8b5dde07d9c7a0744c2c623739896a43b8d2504

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page