简体   繁体   中英

Django model outside of django

I have a non django project for which i would like to use the django models for data access layer.

Added the models lib in requirements.txt
django-model-utils==3.1.1

And code set it up like below:

from django.conf import settings
from django.db import models

settings.configure(
  DATABASE_ENGINE='django.db.backends.mysql',
  DATABASE_NAME='***',
  DATABASE_USER='***',
  DATABASE_PASSWORD='***',
  DATABASE_HOST='***',
  DATABASE_PORT='***')

class Bus(models.Model):
  class Meta:
    db_table = 'my_custom_bus'

  bus_name = models.CharField(max_length=20)
  bus_description = models.CharField(max_length=100)

But when i ran the above code, i am getting the following error: django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

In order to fix the above error, i ran:

import django  
django.setup()

Now when i try, i get:
Bus doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS

Am i missing some setting here or is there any light weight model only lib in python?

(It's an old question, but i answer it, maybe help others.)

option 1 (recommended)

seeing comments, you mentions:

In my particular case, wherein its simply a set of scripts downloading data from apis and saving in database, sqlalchemy is suiting well.

In some situations (like what you mentioned) you can use django management commands. For example if you want to do some tasks that are related to django models and should run in background. like some crontab jobs like updating database fields every 5 minutes or executing some script related to some apis that should run and update database models.

For this, create a management command like below and your good to go:

  • in your app, create a folder management . then add an empty __init__.py file to it. next create another folder called commands in that folder and yet add another empty __init__.py to commands folder. now create your script files in commands folder. for example test_script.py .

  • Now add this in test_script.py :

     from django.core.management.base import BaseCommand class Command(BaseCommand): def handle(self, *args, **kwargs): # Add your script codes here. 
  • Now to run it, simply execute this command: python manage.py test_command

  • for more details read this link

option 2

There is a powerful ORM for python sqlalchemy . You can use it if you don't want to use any part of django or create another django and add your non-django project's code to it. But remember you need to define your django model design with sqlalchemy too. But it's not that hard to do that.

option 3

as my friends suggested in comments, you can config another project to use your existing django project as an app. follow Using Django database layer outside of Django?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM