Skip to content

A library that allows you to use Django's excellent ORM without having to use the rest of Django.

Notifications You must be signed in to change notification settings

petronny/djangorm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DjangORM

A library that allows you to use Django's excellent ORM without having to use the rest of Django.

Dependencies

  • Django

Installation

Just clone the repository to wherever you like.

Usage

  • Setting up the default database (SQLite)
# [your_module_name]/__init__.py
import os
from pathlib import Path
from djangorm import DjangORM

db = DjangORM(module_name=Path(__file__).parent.name)
db.configure()
db.migrate()
  • Setting up a custom database (MySQL)
# [your_module_name]/__init__.py
from djangorm import DjangORM

mysql_config = {
    'ENGINE': 'django.db.backends.mysql',
    'HOST': 'host',
    'NAME': 'name',
    'USER': 'user',
    'PASSWORD': 'password',
}
db = DjangORM(module_name=Path(__file__).parent.name, database=mysql_config)
db.configure()
db.migrate()
  • Define the models:
# [your_module_name]/models.py
from django.db import models

class User(models.Model):
    name = models.CharField(max_length=50, default="")
  • Write your python code
from [your_module_name].models import *

try:
    alice = User.objects.get(name='Alice')
except User.DoesNotExist:
    alice = User(name='Alice')
    alice.save()

for user in User.objects.all():
    print("ID: %d\tUsername: %s" % (user.id, user.name))

Tips

  • If you get errors when doing migrations, try removing [your_module_name]/migrations.

  • There is a function that checking if all the fields are correct.

db.check_models(models)
  • Specify the relative path of the module
db = DjangORM(module_name='[your_module_name]', module_path='[relative_path]')

Acknownledgement

About

A library that allows you to use Django's excellent ORM without having to use the rest of Django.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages