Django

From annawiki

Models

Migrations

Example 1

  • change model type e.g. Char to Integer : psycopg2.errors.InvalidTextRepresentation: invalid input syntax for type integer: ""
  • alter field name in model ...try to delete field ...errors...
  • to fix, try to rename, remove ... delete col by hand using psql, delete migrations, recreate migration ...
  • problems: stuck in errors psycopg2.errors.DuplicateColumn: column "ccc" of relation "rrr" already exists


Example 2

  • change of field to unique can result in "DETAIL: Key (id....)=() is duplicated."

Example 3

Tutorial

https://docs.djangoproject.com/en/4.0/intro/tutorial01/

"We’ll assume you have Django installed already. " - linking to https://docs.djangoproject.com/en/4.0/intro/install/ - venv isn't mentioned, but maybe it is better to do that and not pollute the OS.

django-admin startproject mysite

Shouln't it be myproject instead of mysite, if the command is "startproject"?

The command creates:

mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        asgi.py
        wsgi.py

"The outer mysite/ root directory is a container for your project" - really? What if one had that before (e.g. for data or the venv) and performed "django-admin startproject" within that?

"The inner mysite/ directory is the actual Python package for your project." - the other is the container, this one "the actual Python package for your project".


"June 01, 2022 - 15:50:53" - why this weird data format? How about ISO 8601?

Creating the Polls app
Now that your environment – a “project” – is set up, you’re set to start doing work.

Set up are: 1) a venv 2) "a container for your project" 3) "the actual Python package for your project".


"Projects vs. apps" - "What’s the difference between a project and an app? An app is a web application that does something – e.g., a blog system, a database of public records or a small poll app. A project is a collection of configuration and apps for a particular website. A project can contain multiple apps. An app can be in multiple projects."

So, the outer mysite is "a collection of configuration and apps for a particular website" ... maybe the inner mysite is one of the "multiple apps"? Something like a main app? Or can it be deleted?

"In this tutorial, we’ll create our poll app in the same directory as your manage.py file so that it can be imported as its own top-level module, rather than a submodule of mysite." - the inner mysite?

python manage.py startapp polls

The command creates

polls/
    __init__.py
    admin.py
    apps.py
    migrations/
        __init__.py
    models.py
    tests.py
    views.py

"To call the view, we need to map it to a URL - and for this we need a URLconf." "To create a URLconf in the polls directory, create a file called urls.py." ... There is an urls.py in the inner mysite. Are there apps without views and without urls.py? If every app has its own urls.py then startapp could have created it ... it created views.py!