diff --git a/README.md b/README.md index 7bd3fafa5..b60a83f53 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,23 @@ The development version of this package supports Django 5.0.x. To install it: `pip install git+https://github.com/mongodb-labs/django-mongodb` +### Via templates + +To create a new Django project that uses MongoDB, you can use the templates in this repository. For example: + +```console +$ django-admin startproject --template=project_template mysite +$ django-admin startapp --template=app_template polls +``` + +Then +```console +$ python manage.py makemigrations admin auth contenttypes +$ python manage.py migrate +``` + +### Manual steps + Configure the Django `DATABASES` setting similar to this: ```python diff --git a/app_template/__init__.py-tpl b/app_template/__init__.py-tpl new file mode 100644 index 000000000..e69de29bb diff --git a/app_template/admin.py-tpl b/app_template/admin.py-tpl new file mode 100644 index 000000000..8c38f3f3d --- /dev/null +++ b/app_template/admin.py-tpl @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/app_template/apps.py-tpl b/app_template/apps.py-tpl new file mode 100644 index 000000000..1f5ce786b --- /dev/null +++ b/app_template/apps.py-tpl @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class {{ camel_case_app_name }}Config(AppConfig): + default_auto_field = 'django_mongodb.fields.ObjectIdAutoField' + name = '{{ app_name }}' diff --git a/app_template/migrations/__init__.py-tpl b/app_template/migrations/__init__.py-tpl new file mode 100644 index 000000000..e69de29bb diff --git a/app_template/models.py-tpl b/app_template/models.py-tpl new file mode 100644 index 000000000..71a836239 --- /dev/null +++ b/app_template/models.py-tpl @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/app_template/tests.py-tpl b/app_template/tests.py-tpl new file mode 100644 index 000000000..7ce503c2d --- /dev/null +++ b/app_template/tests.py-tpl @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/app_template/views.py-tpl b/app_template/views.py-tpl new file mode 100644 index 000000000..91ea44a21 --- /dev/null +++ b/app_template/views.py-tpl @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. diff --git a/project_template/manage.py-tpl b/project_template/manage.py-tpl new file mode 100755 index 000000000..a628884dc --- /dev/null +++ b/project_template/manage.py-tpl @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault('DJANGO_SETTINGS_MODULE', '{{ project_name }}.settings') + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == '__main__': + main() diff --git a/project_template/project_name/__init__.py-tpl b/project_template/project_name/__init__.py-tpl new file mode 100644 index 000000000..e69de29bb diff --git a/project_template/project_name/apps.py-tpl b/project_template/project_name/apps.py-tpl new file mode 100644 index 000000000..214cae88e --- /dev/null +++ b/project_template/project_name/apps.py-tpl @@ -0,0 +1,15 @@ +from django.contrib.admin.apps import AdminConfig +from django.contrib.auth.apps import AuthConfig +from django.contrib.contenttypes.apps import ContentTypesConfig + + +class MongoAdminConfig(AdminConfig): + default_auto_field = "django_mongodb.fields.ObjectIdAutoField" + + +class MongoAuthConfig(AuthConfig): + default_auto_field = "django_mongodb.fields.ObjectIdAutoField" + + +class MongoContentTypesConfig(ContentTypesConfig): + default_auto_field = "django_mongodb.fields.ObjectIdAutoField" diff --git a/project_template/project_name/asgi.py-tpl b/project_template/project_name/asgi.py-tpl new file mode 100644 index 000000000..a82723819 --- /dev/null +++ b/project_template/project_name/asgi.py-tpl @@ -0,0 +1,16 @@ +""" +ASGI config for {{ project_name }} project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/{{ docs_version }}/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', '{{ project_name }}.settings') + +application = get_asgi_application() diff --git a/project_template/project_name/settings.py-tpl b/project_template/project_name/settings.py-tpl new file mode 100644 index 000000000..8f993c0a9 --- /dev/null +++ b/project_template/project_name/settings.py-tpl @@ -0,0 +1,131 @@ +""" +Django settings for {{ project_name }} project. + +Generated by 'django-admin startproject' using Django {{ django_version }}. + +For more information on this file, see +https://docs.djangoproject.com/en/{{ docs_version }}/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/{{ docs_version }}/ref/settings/ +""" + +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/{{ docs_version }}/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = '{{ secret_key }}' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + '{{ project_name }}.apps.MongoAdminConfig', + '{{ project_name }}.apps.MongoAuthConfig', + '{{ project_name }}.apps.MongoContentTypesConfig', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = '{{ project_name }}.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = '{{ project_name }}.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/{{ docs_version }}/ref/settings/#databases + +DATABASES = { + "default": { + "ENGINE": "django_mongodb", + "NAME": "my_database", + # "USER": "my_username", + # "PASSWORD": "my_password", + # "OPTIONS": {...}, + }, +} + +# Password validation +# https://docs.djangoproject.com/en/{{ docs_version }}/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/{{ docs_version }}/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/{{ docs_version }}/howto/static-files/ + +STATIC_URL = 'static/' + +# Default primary key field type +# https://docs.djangoproject.com/en/{{ docs_version }}/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django_mongodb.fields.ObjectIdAutoField' + +MIGRATION_MODULES = { + 'admin': '{{ project_name }}.mongo_migrations.admin', + 'auth': '{{ project_name }}.mongo_migrations.auth', + 'contenttypes': '{{ project_name }}.mongo_migrations.contenttypes', +} diff --git a/project_template/project_name/urls.py-tpl b/project_template/project_name/urls.py-tpl new file mode 100644 index 000000000..622f79ef4 --- /dev/null +++ b/project_template/project_name/urls.py-tpl @@ -0,0 +1,22 @@ +""" +URL configuration for {{ project_name }} project. + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/{{ docs_version }}/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" +from django.contrib import admin +from django.urls import path + +urlpatterns = [ + path('admin/', admin.site.urls), +] diff --git a/project_template/project_name/wsgi.py-tpl b/project_template/project_name/wsgi.py-tpl new file mode 100644 index 000000000..1ee28d0e8 --- /dev/null +++ b/project_template/project_name/wsgi.py-tpl @@ -0,0 +1,16 @@ +""" +WSGI config for {{ project_name }} project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/{{ docs_version }}/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', '{{ project_name }}.settings') + +application = get_wsgi_application() diff --git a/pyproject.toml b/pyproject.toml index d06978083..d80a6eac6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -133,3 +133,9 @@ partial_branches = ["if (.*and +)*not _use_c( and.*)*:"] [tool.coverage.html] directory = "htmlcov" + +[tool.setuptools.packages.find] +where = ["."] +include = ["django_mongodb"] +exclude = ["docs", "project_template", "app_template"] +namespaces = false