Use only some parts of Django?

后端 未结 8 1863
北海茫月
北海茫月 2020-11-28 02:39

I like Django, but for a particular application I would like to use only parts of it, but I\'m not familiar enough with how Django works on the inside, so maybe someone can

8条回答
  •  野性不改
    2020-11-28 03:12

    I found KeyboardInterrupt's answer but it was answered in 2009 and I failed to run it in Django 1.8.For recent Django 1.8, You can have a look at this, in which some parts come from KeyboardInterrupt's answer.

    The folder structure is:

    .
    ├── myApp
    │   ├── __init__.py
    │   └── models.py
    └── my_manage.py
    

    myApp is a module, contains an empty __init__.py and models.py.

    There is an example model class in models.py: from django.db import models

    class MyModel(models.Model):
         field = models.CharField(max_length=255)
    

    my_manage.py contains django database, installed_app settings and acts as django offical manage.py, so you can:

    python my_manage.py sql myApp
    python my_manage.py migrate
    ......
    

    The codes in my_manage.py are: from django.conf import settings

    db_conf = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'your_database_name',
            'USER': 'your_user_name',
            'PASSWORD': 'your_password',
            'HOST': 'your_mysql_server_host',
            'PORT': 'your_mysql_server_port',
        }
    }
    
    settings.configure(
        DATABASES = db_conf,
        INSTALLED_APPS     = ( "myApp", )
    )
    
    # Calling django.setup() is required for “standalone” Django u usage
    # https://docs.djangoproject.com/en/1.8/topics/settings/#calling-django-setup-is-required-for-standalone-django-usage
    import django
    django.setup()
    
    if __name__ == '__main__':
        import sys
        from django.core.management import execute_from_command_line
    
        execute_from_command_line(sys.argv)
    

提交回复
热议问题