EveryPage

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 startapp command with the app name. Django creates an app directory structure for the given name in the current directory, and by default that directory contains a models.py file and other app template files. You need to be in the same directory as manage.py to run the command in a project, for example python 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 --template option takes a path to a directory or an archive containing the app template files. Django also accepts http, https, and ftp URLs 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

  • How do I run migrations?

    To run migrations, use the migrate command, which synchronizes the database with your current models and migrations. Create the migration files first with makemigrations, then apply them with migrate.

    The behavior of migrate depends 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 as 0001.
    • 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 given migrate command will perform.
    • --check: makes migrate exit 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 ManyToManyField as 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, a Pizza can have many Topping objects, and a Topping can appear on many pizzas, so you write toppings = models.ManyToManyField(Topping) on Pizza.

    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 ForeignKey apply to the target argument, so the relationship can be recursive (pointing to the same model) or can refer to a model not yet defined. ManyToManyField also 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 through argument.

    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