Django之外的Django模型 [英] Django model outside of django

查看:59
本文介绍了Django之外的Django模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非Django项目,我想使用Django模型进行数据访问层。



中添加了模型库requirements.txt

django-model-utils == 3.1.1



并通过以下代码进行设置:

 来自django.conf导入设置django.db导入模型中的


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)

但是当我运行以上代码时,我出现以下错误:
django.core.exceptions.AppRegistryNotReady:Apps are'



为了解决上述错误,我跑了:



< pre class = lang-python prettyprint-override> import django
django.setup()

现在,当我尝试时,我得到:

公共汽车没有声明显式的app_label,并且不在INSTALLED_APPS中的应用程序中



我在这里缺少某些设置吗?还是只有轻量级模型只能在python中使用lib?

解决方案

(这是一个老问题,但我回答了,也许可以帮助其他人。)



选项1(推荐)



看到评论,您提到:


在这种情况下,sqlalchemy只需一组脚本即可从api下载数据并保存在数据库中,因此非常适合。


就像您提到的一样),您可以使用django管理命令。例如,如果您想执行一些与Django模型相关的任务,并且应在后台运行。例如某些crontab作业,例如每5分钟更新数据库字段或执行一些与应该运行和更新数据库模型的API相关的脚本。



为此,创建一个如下所示的管理命令,您会很高兴:




  • 在您的应用中,创建一个文件夹管理。然后向其中添加一个空的 __ init __。py 文件。接下来在该文件夹中创建另一个名为 commands 的文件夹,然后向中添加另一个空的 __ init __。py 命令文件夹。现在,在 commands 文件夹中创建脚本文件。例如 test_script.py


  • 现在将其添加到 test_script.py中

     从django.core.management.base导入BaseCommand 

    class Command(BaseCommand):

    def handle(self,* args,** kwargs):
    #在此处添加脚本代码。


  • 现在要运行它,只需执行以下命令: python manage .py test_command


  • 有关更多详细信息,请阅读此链接




选项2



对于python sqlalchemy 有强大的ORM。如果您不想使用django的任何部分或不想创建另一个django并向其中添加非django项目的代码,则可以使用它。但是请记住,您也需要使用sqlalchemy定义django模型设计。



选项3



朋友在评论中建议,您可以配置另一个项目以将现有的django项目用作应用程序。按照在Django之外使用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?

这篇关于Django之外的Django模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆