Unofficial example
Ask the Django docs
The Ask this site box, reading docs.djangoproject.com/en/6.1. Type a question and it answers from those pages, with links.
Loading the box…
An unofficial example. Ask this site is not affiliated with or endorsed by the Django project. The Django docs belong to their authors and are read here under their BSD-3-Clause licence.
Questions it answered
Real answers from this box.
How do I create a new app?
To create a new app, run the
startappcommand with the app name. Django creates an app directory structure for the given name in the current directory, and by default that directory contains amodels.pyfile and other app template files. You need to be in the same directory asmanage.pyto run the command in a project, for examplepython manage.py startapp polls.The records describe these options and conditions for
startapp:- Destination directory: If you give a second argument, Django uses that name instead of the current directory, and if the directory doesn't exist it is created. You can use
.to denote the current working directory. Automatic creation of the destination directory was added in a later release. - Custom template: The
--templateoption takes a path to a directory or an archive containing the app template files. Django also acceptshttp,https, andftpURLs to compressed archives, downloading and extracting them on the fly. Templates passed this way are used as is, so their contents should be inspected before use, because malicious or poorly constructed templates may cause security weaknesses or unintended behavior.
The records do not describe a template-based or destination-based workflow beyond these options, so apart from them the basic approach is the single command shown above.
Answered from django-admin and manage.py — django-admin startapp name [directory], Writing your first Django app, part 1 — Creating the Polls app, django-admin and manage.py — --template TEMPLATE
- Destination directory: If you give a second argument, Django uses that name instead of the current directory, and if the directory doesn't exist it is created. You can use
How do I run migrations?
To run migrations, use the
migratecommand, which synchronizes the database with your current models and migrations. Create the migration files first withmakemigrations, then apply them withmigrate.The behavior of
migratedepends on the arguments you pass:- No arguments: runs all migrations for all apps.
<app_label>: runs the migrations for the named app up to its most recent migration, which may also run other apps' migrations because of dependencies.<app_label> <migration_name>: brings the schema to the state where the named migration is applied, without applying later migrations in that app. This can unapply migrations if you had previously migrated past that point. You can use a unique prefix of the name, such as0001.zero: given as the migration name, reverts all applied migrations for the app.
Unapplying migrations also unapplies all dependent migrations, regardless of the app label. To see which migrations would be unapplied, use
--plan.Other options:
--plan: shows the migration operations that the givenmigratecommand will perform.--check: makesmigrateexit with a non-zero status when unapplied migrations are detected.
When you run
migrate, its output lists each migration as it is applied.Answered from django-admin and manage.py — django-admin migrate [app_label] [migration_name], Migrations — The Commands, Django at a glance — Install it, django-admin and manage.py — --plan, django-admin and manage.py — --check, Migrations — Workflow
How do I define a many-to-many relationship?
To define a many-to-many relationship, add a
ManyToManyFieldas a class attribute of your model, just like any other field type. It requires one positional argument, the class to which the model is related. For example, aPizzacan have manyToppingobjects, and aToppingcan appear on many pizzas, so you writetoppings = models.ManyToManyField(Topping)onPizza.Put the field on only one of the two models, not both. Django's documentation generally suggests placing it on the model that will be edited in a form, since users then select the related objects there. The field name is conventionally plural, but this is a suggestion, not a requirement.
The same rules as
ForeignKeyapply to the target argument, so the relationship can be recursive (pointing to the same model) or can refer to a model not yet defined.ManyToManyFieldalso accepts extra optional arguments that control how the relationship works.If you need to store data about the relationship itself, such as a date joined, you can instead name an intermediate model with the
throughargument.Answered from Models — Many-to-many relationships, Model field reference — ManyToManyField, Model field reference — Arguments, Models — Extra fields on many-to-many relationships, Model field reference — ManyToManyField.through
Add this to your own docs
Paste your docs address to try it, free and without signing up. Then it is one line of code.
DocusaurusGitBookMintlifyWordPressReadMeMkDocsVitePressStarlightSphinxHugoJekyllNext.jsGhostWebflow