From 40ffb76320a0c3f02f073419cd95f15ecf549dd7 Mon Sep 17 00:00:00 2001 From: norareidy Date: Tue, 26 Nov 2024 10:21:30 -0500 Subject: [PATCH 01/34] DOCSP-43200: Get started with Django --- snooty.toml | 2 + source/django-get-started.txt | 28 ++++ .../django-connect-mongodb.txt | 89 ++++++++++++ .../django-connection-string.txt | 61 +++++++++ .../django-get-started/django-create-app.txt | 128 ++++++++++++++++++ .../django-create-deployment.txt | 27 ++++ source/django-get-started/django-install.txt | 75 ++++++++++ .../django-get-started/django-write-data.txt | 37 +++++ source/index.txt | 1 + 9 files changed, 448 insertions(+) create mode 100644 source/django-get-started.txt create mode 100644 source/django-get-started/django-connect-mongodb.txt create mode 100644 source/django-get-started/django-connection-string.txt create mode 100644 source/django-get-started/django-create-app.txt create mode 100644 source/django-get-started/django-create-deployment.txt create mode 100644 source/django-get-started/django-install.txt create mode 100644 source/django-get-started/django-write-data.txt diff --git a/snooty.toml b/snooty.toml index de16c44e..7f79d65d 100644 --- a/snooty.toml +++ b/snooty.toml @@ -25,12 +25,14 @@ sharedinclude_root = "https://raw.githubusercontent.com/10gen/docs-shared/main/" driver-short = "PyMongo" driver-long = "PyMongo, the MongoDB synchronous Python driver," driver-async = "PyMongo Async" +django-odm = "Django MongoDB" language = "Python" mdb-server = "MongoDB Server" mongo-community = "MongoDB Community Edition" mongo-enterprise = "MongoDB Enterprise Edition" docs-branch = "master" # always set this to the docs branch (i.e. master, 1.7, 1.8, etc.) version-number = "4.10" +django-version-number = "5.0" patch-version-number = "{+version-number+}.1" # always set this to the driver branch (i.e. 1.7.0, 1.8.0, etc.) version = "v{+version-number+}" stable-api = "Stable API" diff --git a/source/django-get-started.txt b/source/django-get-started.txt new file mode 100644 index 00000000..0be34c27 --- /dev/null +++ b/source/django-get-started.txt @@ -0,0 +1,28 @@ +.. _django-get-started: + +=============================== +Get Started with {+django-odm+} +=============================== + +.. contents:: On this page + :local: + :backlinks: none + :depth: 2 + :class: singlecol + +.. facet:: + :name: genre + :values: tutorial + +.. meta:: + :description: Learn how to create an app to connect to a MongoDB deployment by using Django MongoDB. + :keywords: quick start, tutorial, basics + +.. toctree:: + + Download & Install + Create a Deployment + Create a Connection String + Configure a MongoDB Connection + Create an Application + Write Data to MongoDB \ No newline at end of file diff --git a/source/django-get-started/django-connect-mongodb.txt b/source/django-get-started/django-connect-mongodb.txt new file mode 100644 index 00000000..573dd886 --- /dev/null +++ b/source/django-get-started/django-connect-mongodb.txt @@ -0,0 +1,89 @@ +.. _django-get-started-connect: + +================================= +Configure your MongoDB Connection +================================= + +.. facet:: + :name: genre + :values: tutorial + +.. meta:: + :keywords: app, odm, code example + +.. procedure:: + :style: connected + + .. step:: Create a Django project + + From your shell, run the following command to create a + new Django project called ``quickstart`` based on a custom template: + + .. code-block:: bash + + django-admin startproject quickstart --template https://github.com/mongodb-labs/django-mongodb-project/archive/refs/heads/{+django-version-number+}.x.zip + + .. note:: Project Template + + The ``django-mongodb-project`` template resembles the default Django project + template but makes the following changes: + + - Includes MongoDB-specific migrations + - Modifies the ``settings.py`` file to instruct Django + to use an ``ObjectId`` value as each model's primary key + + After running this command, your ``quickstart`` project has + the following file structure: + + .. code-block:: bash + + quickstart/ + manage.py + mongo_migrations/ + __init__.py + contenttypes/ + auth/ + admin/ + quickstart/ + __init__.py + apps.py + settings.py + urls.py + asgi.py + wsgi.py + + .. step:: Update your database settings + + Open your ``settings.py`` file and navigate to the ``DATABASES`` setting. + Replace this setting with the following code: + + .. code-block:: bash + + DATABASES = { + "default": { + "ENGINE": "django_mongodb", + "NAME": "db", + "USER": "", + "PASSWORD": "", + "HOST": "", + }, + } + + Replace the ```` and ```` placeholders with your + Atlas database user's username and password. Then, replace the + ```` placeholder with the connection string + that you copied from the :ref:`django-get-started-connection-string` + step of this guide. + + .. step:: Start the server + + To verify that you installed {+django-odm+} and correctly configured + your project, run the following command from your project root: + + .. code-block:: bash + + python3 manage.py runserver + + Then, visit http://127.0.0.1:8000/. You should see a "Congratulations!" + message and an image of a rocket. + diff --git a/source/django-get-started/django-connection-string.txt b/source/django-get-started/django-connection-string.txt new file mode 100644 index 00000000..2d9c61bf --- /dev/null +++ b/source/django-get-started/django-connection-string.txt @@ -0,0 +1,61 @@ +.. _django-get-started-connection-string: + +========================== +Create a Connection String +========================== + +You can connect to your MongoDB deployment by providing a +**connection URI**, also called a *connection string*, which +instructs the driver on how to connect to a MongoDB deployment +and how to behave while connected. + +The connection string includes the hostname or IP address and +port of your deployment, the authentication mechanism, user credentials +when applicable, and connection options. + +To connect to an instance or deployment not hosted on Atlas, see +:ref:`pymongo-connection-targets` in the PyMongo documentation. + +.. procedure:: + :style: connected + + .. step:: Find your MongoDB Atlas Connection String + + To retrieve your connection string for the deployment that + you created in the :ref:`previous step `, + log into your Atlas account and navigate to the + :guilabel:`Database` section and click the :guilabel:`Connect` button + for your new deployment. + + .. figure:: /includes/figures/atlas_connection_select_cluster.png + :alt: The connect button in the clusters section of the Atlas UI + + Proceed to the :guilabel:`Connect your application` section and select + "Python" from the :guilabel:`Driver` selection menu and the version + that best matches the version you installed from the :guilabel:`Version` + selection menu. + + Select the :guilabel:`Password (SCRAM)` authentication mechanism. + + Deselect the :guilabel:`Include full driver code example` option to view + the connection string. + + .. step:: Copy your Connection String + + Click the button on the right of the connection string to copy it to + your clipboard as shown in the following screenshot: + + .. figure:: /includes/figures/atlas_connection_copy_string_python.png + :alt: The connection string copy button in the Atlas UI + + .. step:: Update the Placeholders + + Paste this connection string into a file in your preferred text editor + and replace the ```` and ```` placeholders with + your database user's username and password. + + Save this file to a safe location for use in the next step. + +After completing these steps, you have a connection string that +contains your database username and password. + diff --git a/source/django-get-started/django-create-app.txt b/source/django-get-started/django-create-app.txt new file mode 100644 index 00000000..f46f0938 --- /dev/null +++ b/source/django-get-started/django-create-app.txt @@ -0,0 +1,128 @@ +.. _django-get-started-create-app: + +===================== +Create an Application +===================== + +.. facet:: + :name: genre + :values: tutorial + +.. meta:: + :keywords: app, odm, code example + +.. procedure:: + :style: connected + + .. step:: Create a "sample_mflix" app + + From your ``quickstart`` project directory, run the following command to create a + new Django app called ``sample_mflix`` based on a custom template: + + .. code-block:: bash + + python3 manage.py startapp sample_mflix --template https://github.com/mongodb-labs/django-mongodb-app/archive/refs/heads/5.0.x.zip + + .. note:: App Template + + The ``django-mongodb-app`` template ensures that your ``app.py`` file + includes the line ``"default_auto_field = 'django_mongodb.fields.ObjectIdAutoField'"``. + + .. step:: Create a view + + Open the ``views.py`` file in your ``sample_mflix`` directory and replace + its contents with the following code: + + .. code-block:: python + + from django.http import HttpResponse + + + def index(request): + return HttpResponse("Hello, world. You're at the application index.") + + .. step:: Configure a URL + + Create a new file called ``urls.py`` file in your ``sample_mflix`` directory and add + the following code: + + .. code-block:: python + + from django.urls import path + + from . import views + + urlpatterns = [ + path("", views.index, name="index"), + ] + + Then, navigate to the ``quickstart/urls.py`` file and replace its contents with + the following code: + + .. code-block:: python + + from django.contrib import admin + from django.urls import include, path + + urlpatterns = [ + path("sample_mflix/", include("sample_mflix.urls")), + path("admin/", admin.site.urls), + ] + + Visit http://127.0.0.1:8000/sample_mflix/ to see the text defined in your view. + + .. step:: Apply database migrations + + From your project root, run the following command: + + .. code-block:: bash + + python manage.py migrate + + .. step:: Create models for movie and users data + + Open the ``models.py`` file in the ``sample_mflix`` directory and replace + its contents with the following code: + + .. code-block:: python + + from django.db import models + + + class Movies(models.Model): + name = models.CharField(max_length=200) + runtime = models.IntegerField(default=0) + + + class Users(models.Model): + name = models.CharField(max_length=200) + email = models.CharField(max_length=200) + + + .. step:: Include your app in your project + + Open the ``settings.py`` file in ``quickstart`` and edit your + ``INSTALLED_APPS`` setting to resemble the following code: + + .. code-block:: python + + INSTALLED_APPS = [ + 'sample_mflix.apps.SampleMflixConfig', + 'quickstart.apps.MongoAdminConfig', + 'quickstart.apps.MongoAuthConfig', + 'quickstart.apps.MongoContentTypesConfig', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + ] + + .. step:: Create migrations for your new models + + From your project root, run the following command to create + migrations for the ``Movies`` and ``Users`` models and Apply + the changes to the database: + + .. code-block:: bash + + python3 manage.py makemigrations sample_mflix + python3 manage.py migrate diff --git a/source/django-get-started/django-create-deployment.txt b/source/django-get-started/django-create-deployment.txt new file mode 100644 index 00000000..ad290be5 --- /dev/null +++ b/source/django-get-started/django-create-deployment.txt @@ -0,0 +1,27 @@ +.. _django-get-started-create-deployment: + +=========================== +Create a MongoDB Deployment +=========================== + +You can create a free tier MongoDB deployment on MongoDB Atlas +to store and manage your data. MongoDB Atlas hosts and manages +your MongoDB database in the cloud. + +.. procedure:: + :style: connected + + .. step:: Create a Free MongoDB deployment on Atlas + + Complete the :atlas:`Get Started with Atlas ` + guide to set up a new Atlas account and load sample data into a new free + tier MongoDB deployment. + + .. step:: Save your Credentials + + After you create your database user, save that user's + username and password to a safe location for use in an upcoming step. + +After you complete these steps, you have a new free tier MongoDB +deployment on Atlas, database user credentials, and sample data loaded +in your database. \ No newline at end of file diff --git a/source/django-get-started/django-install.txt b/source/django-get-started/django-install.txt new file mode 100644 index 00000000..e2cf27eb --- /dev/null +++ b/source/django-get-started/django-install.txt @@ -0,0 +1,75 @@ +.. _django-get-started-download-and-install: + +==================== +Download and Install +==================== + +.. facet:: + :name: genre + :values: tutorial + +.. meta:: + :keywords: setup, odm, code example + +.. procedure:: + :style: connected + + .. step:: Install Dependencies + + Before installing {+django-odm+}, ensure you have the following dependencies installed + in your development environment: + + - `Python3 version 3.10 or later `__ + - `pip `__ + + .. step:: Create a virtual environment + + Select the tab corresponding to your operating system and run the following commands + to create and activate a virtual environment in which to install {+django-odm+}: + + .. tabs:: + + .. tab:: macOS / Linux + :tabid: mac-linux-venv + + .. code-block:: bash + + python3 -m venv venv + source venv/bin/activate + + .. tab:: Windows + :tabid: windows-venv + + .. code-block:: bash + + python3 -m venv venv + . venv\Scripts\activate + + .. step:: Install {+django-odm+} + + With the virtual environment activated, run the following command to install + the Django integration: + + .. code-block:: bash + + pip install git+https://github.com/mongodb-labs/django-mongodb + + This command also installs the following dependencies: + + - PyMongo version {+version-number+} + - Django version {+django-version-number+} + + .. important:: + + If you have already installed PyMongo and Django in your + development environment, ensure that they fulfill the following + version requirements: + + - PyMongo: Your version must be greater than or equal to 4.6 and less than 5.0. + - Django: Your version must be greater than or equal to 5.0 and less than 5.1. + + +After you complete these steps, you have {+django-odm+} and its +dependencies installed in your development environment. + + diff --git a/source/django-get-started/django-write-data.txt b/source/django-get-started/django-write-data.txt new file mode 100644 index 00000000..57395b94 --- /dev/null +++ b/source/django-get-started/django-write-data.txt @@ -0,0 +1,37 @@ +.. _django-get-started-create-app: + +===================== +Write Data to MongoDB +===================== + +.. facet:: + :name: genre + :values: tutorial + +.. meta:: + :keywords: app, odm, code example + +.. procedure:: + :style: connected + + .. step:: Start a Python shell + + From your ``quickstart`` project directory, run the following command to + enter the Python shell: + + .. code-block:: bash + + python3 manage.py shell + + .. step:: Insert a movie into the database + + From your Python shell, run the following commands: + + .. code-block:: python + + from sample_mflix.models import Movies, Users + parasite = Movies(name="Parasite", runtime=132) + parasite.save() + + This code inserts a movie with a ``name`` value of ``"Parasite"`` into + the ``sample_mflix`` MongoDB database. \ No newline at end of file diff --git a/source/index.txt b/source/index.txt index 8ca66669..5ea14726 100644 --- a/source/index.txt +++ b/source/index.txt @@ -13,6 +13,7 @@ MongoDB {+driver-short+} Documentation .. toctree:: Get Started + Get Started with Django MongoDB Connect Databases & Collections Write Data From be878e53e7ffb0de2abe560bbe512ad30c726ac5 Mon Sep 17 00:00:00 2001 From: norareidy Date: Tue, 26 Nov 2024 10:38:22 -0500 Subject: [PATCH 02/34] edits --- source/django-get-started.txt | 4 +++- .../django-get-started/django-connect-mongodb.txt | 1 + source/django-get-started/django-create-app.txt | 2 +- source/django-get-started/django-next-steps.txt | 15 +++++++++++++++ source/django-get-started/django-view-data.txt | 12 ++++++++++++ 5 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 source/django-get-started/django-next-steps.txt create mode 100644 source/django-get-started/django-view-data.txt diff --git a/source/django-get-started.txt b/source/django-get-started.txt index 0be34c27..1d210d62 100644 --- a/source/django-get-started.txt +++ b/source/django-get-started.txt @@ -25,4 +25,6 @@ Get Started with {+django-odm+} Create a Connection String Configure a MongoDB Connection Create an Application - Write Data to MongoDB \ No newline at end of file + Write Data to MongoDB + View MongoDB Data + Next Steps \ No newline at end of file diff --git a/source/django-get-started/django-connect-mongodb.txt b/source/django-get-started/django-connect-mongodb.txt index 573dd886..49920329 100644 --- a/source/django-get-started/django-connect-mongodb.txt +++ b/source/django-get-started/django-connect-mongodb.txt @@ -36,6 +36,7 @@ Configure your MongoDB Connection the following file structure: .. code-block:: bash + :copyable: false quickstart/ manage.py diff --git a/source/django-get-started/django-create-app.txt b/source/django-get-started/django-create-app.txt index f46f0938..ade82c71 100644 --- a/source/django-get-started/django-create-app.txt +++ b/source/django-get-started/django-create-app.txt @@ -119,7 +119,7 @@ Create an Application .. step:: Create migrations for your new models From your project root, run the following command to create - migrations for the ``Movies`` and ``Users`` models and Apply + migrations for the ``Movies`` and ``Users`` models and apply the changes to the database: .. code-block:: bash diff --git a/source/django-get-started/django-next-steps.txt b/source/django-get-started/django-next-steps.txt new file mode 100644 index 00000000..b4a4a8d8 --- /dev/null +++ b/source/django-get-started/django-next-steps.txt @@ -0,0 +1,15 @@ +.. _django-get-started-next-steps: + +========== +Next Steps +========== + +Congratulations on completing the {+django-odm+} tutorial! + +In this tutorial, you created a Django application that +connects to a MongoDB deployment hosted on MongoDB Atlas +and interacts with data. + +Learn more about {+django-odm+} from the following resources: + +- :github:`django-mongodb ` source code \ No newline at end of file diff --git a/source/django-get-started/django-view-data.txt b/source/django-get-started/django-view-data.txt new file mode 100644 index 00000000..ebb6fc5a --- /dev/null +++ b/source/django-get-started/django-view-data.txt @@ -0,0 +1,12 @@ +.. _django-get-started-create-app: + +================= +View MongoDB Data +================= + +.. facet:: + :name: genre + :values: tutorial + +.. meta:: + :keywords: app, odm, code example \ No newline at end of file From 704c54efa9a1a852bfb12ede724c86344518b0b5 Mon Sep 17 00:00:00 2001 From: norareidy Date: Tue, 26 Nov 2024 11:51:18 -0500 Subject: [PATCH 03/34] edit code --- source/django-get-started/django-create-app.txt | 15 +++++++++------ source/django-get-started/django-write-data.txt | 4 ++-- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/source/django-get-started/django-create-app.txt b/source/django-get-started/django-create-app.txt index ade82c71..b7b998ed 100644 --- a/source/django-get-started/django-create-app.txt +++ b/source/django-get-started/django-create-app.txt @@ -77,27 +77,30 @@ Create an Application .. code-block:: bash - python manage.py migrate + python3 manage.py migrate - .. step:: Create models for movie and users data + .. step:: Create models for movie and user data Open the ``models.py`` file in the ``sample_mflix`` directory and replace its contents with the following code: .. code-block:: python + import datetime + from django.db import models class Movies(models.Model): - name = models.CharField(max_length=200) + title = models.CharField(max_length=200) + plot = models.CharField(max_length=800) runtime = models.IntegerField(default=0) - + released = models.DateTimeField("release date", null=True) class Users(models.Model): - name = models.CharField(max_length=200) + name = models.CharField(max_length=100) email = models.CharField(max_length=200) - + password = models.CharField(max_length=100) .. step:: Include your app in your project diff --git a/source/django-get-started/django-write-data.txt b/source/django-get-started/django-write-data.txt index 57395b94..38493070 100644 --- a/source/django-get-started/django-write-data.txt +++ b/source/django-get-started/django-write-data.txt @@ -30,8 +30,8 @@ Write Data to MongoDB .. code-block:: python from sample_mflix.models import Movies, Users - parasite = Movies(name="Parasite", runtime=132) + parasite = Movies(title="Parasite", runtime=132) parasite.save() - This code inserts a movie with a ``name`` value of ``"Parasite"`` into + This code inserts a movie with a ``title`` value of ``"Parasite"`` into the ``sample_mflix`` MongoDB database. \ No newline at end of file From 1caf1a3848eee19afb0e973feb95dd25ccabec28 Mon Sep 17 00:00:00 2001 From: norareidy Date: Wed, 4 Dec 2024 16:42:33 -0500 Subject: [PATCH 04/34] more info --- .../django-get-started/django-create-app.txt | 158 ++++++++++++++---- .../django-get-started/django-write-data.txt | 61 ++++++- 2 files changed, 183 insertions(+), 36 deletions(-) diff --git a/source/django-get-started/django-create-app.txt b/source/django-get-started/django-create-app.txt index b7b998ed..64c8c6fd 100644 --- a/source/django-get-started/django-create-app.txt +++ b/source/django-get-started/django-create-app.txt @@ -28,7 +28,47 @@ Create an Application The ``django-mongodb-app`` template ensures that your ``app.py`` file includes the line ``"default_auto_field = 'django_mongodb.fields.ObjectIdAutoField'"``. - .. step:: Create a view + .. step:: Create models for movie and user data + + Open the ``models.py`` file in the ``sample_mflix`` directory and replace + its contents with the following code: + + .. code-block:: python + + from django.db import models + + class Movies(models.Model): + title = models.CharField(max_length=200) + plot = models.TextField(null=True) + runtime = models.IntegerField(default=0) + released = models.DateTimeField("release date", null=True) + + class Meta: + managed = False + db_table = "movies" + + def __str__(self): + return self.title + + + class Users(models.Model): + name = models.CharField(max_length=100) + email = models.CharField(max_length=200) + password = models.CharField(max_length=100) + + class Meta: + managed = False + db_table = "users" + + def __str__(self): + return self.name + + The ``Movies`` model represents the ``sample_mflix.movies`` collection + and stores information about movies. The ``Users`` model represents + the ``sample_mflix.users`` collection and stores account credentials + for users of a movie streaming platform. + + .. step:: Create views to display data Open the ``views.py`` file in your ``sample_mflix`` directory and replace its contents with the following code: @@ -36,15 +76,29 @@ Create an Application .. code-block:: python from django.http import HttpResponse + from django.shortcuts import render + from .models import Movies, Users def index(request): - return HttpResponse("Hello, world. You're at the application index.") + return HttpResponse("Hello! You've reached the Django MongoDB sample app landing page.") - .. step:: Configure a URL + def recent_movies(request): + movies = Movies.objects.all().order_by('-released')[:3] + return render(request, 'recent_movies.html', {'movies': movies}) - Create a new file called ``urls.py`` file in your ``sample_mflix`` directory and add - the following code: + def users_list(request): + users = Users.objects.all().order_by('name')[:10] + return render(request, 'users_list.html', {'users': users}) + + These views display a landing page message and information about your ``Movies`` + and ``Users`` models. + + .. step:: Configure URLs for your views + + Create a new file called ``urls.py`` file in your ``sample_mflix`` directory. + To map the views defined in the preceding steps to URLs, paste the following + code into ``urls.py``: .. code-block:: python @@ -54,6 +108,8 @@ Create an Application urlpatterns = [ path("", views.index, name="index"), + path('recent_movies/', views.recent_movies, name='recent_movies'), + path('users_list/', views.users_list, name='users_list'), ] Then, navigate to the ``quickstart/urls.py`` file and replace its contents with @@ -69,38 +125,77 @@ Create an Application path("admin/", admin.site.urls), ] - Visit http://127.0.0.1:8000/sample_mflix/ to see the text defined in your view. + .. step:: Create a template to format your views - .. step:: Apply database migrations - - From your project root, run the following command: + In your ``sample_mflix`` directory, create a subdirectory called + ``templates``. Then, create a file called ``recent_movies.html`` + and paste the following code: - .. code-block:: bash - - python3 manage.py migrate - - .. step:: Create models for movie and user data - - Open the ``models.py`` file in the ``sample_mflix`` directory and replace - its contents with the following code: - .. code-block:: python - import datetime - - from django.db import models + + + + + + + Recent Movies + + +

Three Most Recent Movies

+
    + {% for movie in movies %} +
  • + {{ movie.title }} (Released: {{ movie.released }}) +
  • + {% empty %} +
  • No movies found.
  • + {% endfor %} +
+ + + + This template formats the movie data requested by the ``recent_movies`` view. + Create another file in the ``sample_mflix/templates`` directory called + ``users_list.html`` and paste the following code: - class Movies(models.Model): - title = models.CharField(max_length=200) - plot = models.CharField(max_length=800) - runtime = models.IntegerField(default=0) - released = models.DateTimeField("release date", null=True) + .. code-block:: python - class Users(models.Model): - name = models.CharField(max_length=100) - email = models.CharField(max_length=200) - password = models.CharField(max_length=100) + + + + + + + Users List + + +

Users List

+ + + + + + + + + {% for user in users %} + + + + + {% empty %} + + + + {% endfor %} + +
NameEmail
{{ user.name }}{{ user.email }}
No users found.
+ + + + This template formats the user data requested by the ``users_list`` view. .. step:: Include your app in your project @@ -129,3 +224,6 @@ Create an Application python3 manage.py makemigrations sample_mflix python3 manage.py migrate + +After completing these steps, you have a basic {+django-odm+} app that +you can use to interact with the ``sample_mflix`` database. \ No newline at end of file diff --git a/source/django-get-started/django-write-data.txt b/source/django-get-started/django-write-data.txt index 38493070..06073e23 100644 --- a/source/django-get-started/django-write-data.txt +++ b/source/django-get-started/django-write-data.txt @@ -23,15 +23,64 @@ Write Data to MongoDB python3 manage.py shell - .. step:: Insert a movie into the database + .. step:: Import the required classes and modules - From your Python shell, run the following commands: + From your Python shell, run the following code to import + your models and the modules for creating a ``datetime`` object: .. code-block:: python from sample_mflix.models import Movies, Users - parasite = Movies(title="Parasite", runtime=132) - parasite.save() + from django.utils import timezone + from datetime import datetime + + .. step:: Insert a movie into the database + + Run the following code to create a ``Movies`` model that + stores data about a movie titled ``"Minari"``: + + .. code-block:: python + + movie = Movies(title="Minari", plot= + "A Korean-American family moves to an Arkansas farm in search of their own American Dream", + runtime=117, released=timezone.make_aware(datetime(2020, 1, 26, 0, 0), + timezone.get_current_timezone())) + + Save the model to store its data as a document in the ``movies`` + collection, as shown in the following code: + + .. code-block:: python + + movie.save() + + .. step:: Insert a user into the database + + Run the following code to create a ``Users`` model that + stores data about a user named ``"Abigail Carter"``: + + .. code-block:: python + + user = Users(name="Abigail Carter", email="abigail.carter@fakegmail.com", + password="secure123") + + Save the model to store its data as a document in the ``users`` + collection, as shown in the following code: + + .. code-block:: python + + user.save() + + .. step:: Render your new models + + To ensure that your ``Movies`` model was inserted into the database, + visit the http://127.0.0.1:8000/sample_mflix/recent_movies/ URL. + You should see a list of five movies in the ``sample_mflix.movies`` + database, with your new movie listed at the top. + + Then, ensure that your ``Users`` model was inserted into the + database by visiting the http://127.0.0.1:8000/sample_mflix/users_list/ + URL. You should see a list of ten users in the ``sample_mflix.users`` + database, with your new user listed at the top. - This code inserts a movie with a ``title`` value of ``"Parasite"`` into - the ``sample_mflix`` MongoDB database. \ No newline at end of file +After completing these steps, you have new documents in the ``movies`` +and ``users`` collection in the ``sample_mflix`` sample database. \ No newline at end of file From 4142196c767da20ab19d97b99276106f32df458d Mon Sep 17 00:00:00 2001 From: norareidy Date: Wed, 4 Dec 2024 16:56:48 -0500 Subject: [PATCH 05/34] query steps --- .../django-get-started/django-query-data.txt | 54 +++++++++++++++++++ .../django-get-started/django-view-data.txt | 12 ----- 2 files changed, 54 insertions(+), 12 deletions(-) create mode 100644 source/django-get-started/django-query-data.txt delete mode 100644 source/django-get-started/django-view-data.txt diff --git a/source/django-get-started/django-query-data.txt b/source/django-get-started/django-query-data.txt new file mode 100644 index 00000000..79bc6db2 --- /dev/null +++ b/source/django-get-started/django-query-data.txt @@ -0,0 +1,54 @@ +.. _django-get-started-create-app: + +================== +Query MongoDB Data +================== + +.. facet:: + :name: genre + :values: tutorial + +.. meta:: + :keywords: app, odm, code example + +.. step:: Query the users collection for a specified email + + From the same Python shell as the :ref:`django-get-started-create-app` + step of this tutorial, run the following code to query the + ``sample_mflix.users`` collection for a user whose email is + ``"jason_momoa@gameofthron.es"``: + + .. code-block:: bash + + Users.objects.filter(email="jason_momoa@gameofthron.es").first() + + This code returns the name of the matching user: + + .. code-block:: bash + + ]> + +.. step:: Query the movies collection for runtime values + + Run the following code to query the ``sample_mflix.movies`` + collection for movies that have a runtime value less than + 10: + + .. code-block:: bash + + Movies.objects.filter(runtime__lt=10) + + This code returns a truncated list of the matching movies: + + .. code-block:: bash + + , , , , , + , , , + , , , + , , + , , + , , + , , + , '...(remaining elements truncated)...']> diff --git a/source/django-get-started/django-view-data.txt b/source/django-get-started/django-view-data.txt deleted file mode 100644 index ebb6fc5a..00000000 --- a/source/django-get-started/django-view-data.txt +++ /dev/null @@ -1,12 +0,0 @@ -.. _django-get-started-create-app: - -================= -View MongoDB Data -================= - -.. facet:: - :name: genre - :values: tutorial - -.. meta:: - :keywords: app, odm, code example \ No newline at end of file From aa27f1263ab2f917bbc503d2900a375911f402d5 Mon Sep 17 00:00:00 2001 From: norareidy Date: Wed, 4 Dec 2024 16:58:57 -0500 Subject: [PATCH 06/34] edit --- source/django-get-started/django-create-app.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/django-get-started/django-create-app.txt b/source/django-get-started/django-create-app.txt index 64c8c6fd..838fe7ca 100644 --- a/source/django-get-started/django-create-app.txt +++ b/source/django-get-started/django-create-app.txt @@ -84,7 +84,7 @@ Create an Application return HttpResponse("Hello! You've reached the Django MongoDB sample app landing page.") def recent_movies(request): - movies = Movies.objects.all().order_by('-released')[:3] + movies = Movies.objects.all().order_by('-released')[:5] return render(request, 'recent_movies.html', {'movies': movies}) def users_list(request): @@ -142,7 +142,7 @@ Create an Application Recent Movies -

Three Most Recent Movies

+

Five Most Recent Movies