DjangAutomate API Reference
DjangAutomate
A framework to automate Django app, model, serializer, and view generation from SQLAlchemy database tables.
Modules:
core : Main class for automating Django app creation.
generators : Classes for generating models, views, serializers, and app config.
utils : Helper functions for updating URLs and settings.
Usage:
>>> from djangautomate import Djangautomate
>>> automator = Djangautomate("sqlite:///example.db", "users", app_name="my_app")
>>> automator.generate_code_files()
- class djangautomate.Djangautomate(db_engine, table_name, index_cols=None, app_name='', project_name='')[source]
Bases:
objectAutomates Django app creation by generating models, views, serializers, templates, and updating configurations.
- db_engine
The SQLAlchemy database engine.
- Type:
sqlalchemy.engine.Engine
- table_name
The name of the table to generate models from.
- Type:
str
- index_cols
List of indexed columns. Defaults to an empty list.
- Type:
list[str], optional
- app_name
The Django app name.
- Type:
str
- project_name
The Django project name.
- Type:
str
- camelcased_app_name
The formatted app name in CamelCase.
- Type:
str
- viewset_name
The generated name for the Django ViewSet.
- Type:
str
- serializer_name
The generated name for the Django Serializer.
- Type:
str
- __dict__ = mappingproxy({'__module__': 'djangautomate.core', '__doc__': '\n Automates Django app creation by generating models, views, serializers, templates, and updating configurations.\n\n Attributes:\n db_engine (sqlalchemy.engine.Engine): The SQLAlchemy database engine.\n table_name (str): The name of the table to generate models from.\n index_cols (list[str], optional): List of indexed columns. Defaults to an empty list.\n app_name (str): The Django app name.\n project_name (str): The Django project name.\n camelcased_app_name (str): The formatted app name in CamelCase.\n viewset_name (str): The generated name for the Django ViewSet.\n serializer_name (str): The generated name for the Django Serializer.\n ', '__init__': <function Djangautomate.__init__>, 'create_django_app': <function Djangautomate.create_django_app>, 'generate_code_files': <function Djangautomate.generate_code_files>, '_write_file': <function Djangautomate._write_file>, '__dict__': <attribute '__dict__' of 'Djangautomate' objects>, '__weakref__': <attribute '__weakref__' of 'Djangautomate' objects>, '__annotations__': {}})
- __init__(db_engine, table_name, index_cols=None, app_name='', project_name='')[source]
Initializes the Djangautomate class.
- Parameters:
db_engine (sqlalchemy.engine.Engine) – The SQLAlchemy database engine.
table_name (str) – The name of the SQLAlchemy table.
index_cols (list[str], optional) – List of indexed columns. Defaults to an empty list.
app_name (str, optional) – The Django app name. Defaults to an empty string.
project_name (str, optional) – The Django project name. Defaults to an empty string.
- __module__ = 'djangautomate.core'
- __weakref__
list of weak references to the object
- create_django_app()[source]
Creates a new Django app using django-admin startapp.
This function runs the Django CLI command to initialize a new app inside the current project.
- generate_code_files()[source]
Generates all necessary Django files, including models, views, serializers, and app configuration.
This function: - Creates the Django app directory (if it doesn’t exist). - Generates the model, view, and serializer code using ModelGenerator, ViewGenerator, and SerializerGenerator. - Writes the generated code to respective files inside the app directory. - Updates Django’s urls.py and settings.py to include the new app.
- Raises:
Exception – If the Django app directory cannot be created.
- class djangautomate.ModelGenerator(db_engine, table_name, index_cols)[source]
Bases:
objectGenerates Django model code from an SQLAlchemy table.
- db_engine
The SQLAlchemy database engine.
- Type:
sqlalchemy.engine.Engine
- table_name
The name of the SQLAlchemy table to convert to a Django model.
- Type:
str
- index_cols
A list of indexed columns.
- Type:
list[str]
- __dict__ = mappingproxy({'__module__': 'djangautomate.generators', '__doc__': '\n Generates Django model code from an SQLAlchemy table.\n\n Attributes:\n db_engine (sqlalchemy.engine.Engine): The SQLAlchemy database engine.\n table_name (str): The name of the SQLAlchemy table to convert to a Django model.\n index_cols (list[str]): A list of indexed columns.\n ', '__init__': <function ModelGenerator.__init__>, 'generate': <function ModelGenerator.generate>, 'get_field_type': <function ModelGenerator.get_field_type>, '__dict__': <attribute '__dict__' of 'ModelGenerator' objects>, '__weakref__': <attribute '__weakref__' of 'ModelGenerator' objects>, '__annotations__': {}})
- __init__(db_engine, table_name, index_cols)[source]
Initializes the ModelGenerator class.
- Parameters:
db_engine (sqlalchemy.engine.Engine) – The SQLAlchemy database engine.
table_name (str) – The name of the table to generate the model from.
index_cols (list[str]) – A list of indexed columns.
- __module__ = 'djangautomate.generators'
- __weakref__
list of weak references to the object
- class djangautomate.ViewGenerator(app_name: str)[source]
Bases:
objectGenerates Django ViewSet code for the given model.
- app_name
The name of the Django app.
- Type:
str
- camelcased_app_name
The app name in CamelCase format.
- Type:
str
- __dict__ = mappingproxy({'__module__': 'djangautomate.generators', '__doc__': '\n Generates Django ViewSet code for the given model.\n\n Attributes:\n app_name (str): The name of the Django app.\n camelcased_app_name (str): The app name in CamelCase format.\n ', '__init__': <function ViewGenerator.__init__>, 'generate': <function ViewGenerator.generate>, '__dict__': <attribute '__dict__' of 'ViewGenerator' objects>, '__weakref__': <attribute '__weakref__' of 'ViewGenerator' objects>, '__annotations__': {}})
- __init__(app_name: str)[source]
Initializes the ViewGenerator class.
- Parameters:
app_name (str) – The name of the Django app.
- __module__ = 'djangautomate.generators'
- __weakref__
list of weak references to the object
- class djangautomate.SerializerGenerator(db_engine, table_name: str)[source]
Bases:
objectGenerates Django REST Framework serializer code.
- db_engine
The SQLAlchemy database engine.
- Type:
sqlalchemy.engine.Engine
- table_name
The name of the SQLAlchemy table to generate a serializer for.
- Type:
str
- __dict__ = mappingproxy({'__module__': 'djangautomate.generators', '__doc__': '\n Generates Django REST Framework serializer code.\n\n Attributes:\n db_engine (sqlalchemy.engine.Engine): The SQLAlchemy database engine.\n table_name (str): The name of the SQLAlchemy table to generate a serializer for.\n ', '__init__': <function SerializerGenerator.__init__>, 'generate': <function SerializerGenerator.generate>, '__dict__': <attribute '__dict__' of 'SerializerGenerator' objects>, '__weakref__': <attribute '__weakref__' of 'SerializerGenerator' objects>, '__annotations__': {}})
- __init__(db_engine, table_name: str)[source]
Initializes the SerializerGenerator class.
- Parameters:
db_engine (sqlalchemy.engine.Engine) – The SQLAlchemy database engine.
table_name (str) – The name of the table to generate the serializer for.
- __module__ = 'djangautomate.generators'
- __weakref__
list of weak references to the object
- class djangautomate.AppConfigGenerator(app_name: str)[source]
Bases:
objectGenerates Django app configuration.
- app_name
The name of the Django app.
- Type:
str
- __dict__ = mappingproxy({'__module__': 'djangautomate.generators', '__doc__': '\n Generates Django app configuration.\n\n Attributes:\n app_name (str): The name of the Django app.\n ', '__init__': <function AppConfigGenerator.__init__>, 'generate': <function AppConfigGenerator.generate>, '__dict__': <attribute '__dict__' of 'AppConfigGenerator' objects>, '__weakref__': <attribute '__weakref__' of 'AppConfigGenerator' objects>, '__annotations__': {}})
- __init__(app_name: str)[source]
Initializes the AppConfigGenerator class.
- Parameters:
app_name (str) – The name of the Django app.
- __module__ = 'djangautomate.generators'
- __weakref__
list of weak references to the object
- djangautomate.update_urls(app_name: str, viewset_name: str)[source]
Updates Django’s urls.py to include the newly generated app’s ViewSet.
This function appends the required import statement and router registration for the new Django app’s view.
- Parameters:
app_name (str) – The name of the Django app.
viewset_name (str) – The name of the ViewSet class.
- Raises:
FileNotFoundError – If urls.py is not found in the expected path.
- djangautomate.update_settings(app_name: str, camelcased_app_name: str)[source]
Adds the generated Django app to INSTALLED_APPS in settings.py.
This function appends the app configuration class to Django’s INSTALLED_APPS list, ensuring it is registered correctly.
- Parameters:
app_name (str) – The name of the Django app.
camelcased_app_name (str) – The app name in CamelCase format for the config class.
- Raises:
FileNotFoundError – If settings.py is not found in the expected path.