Herramientas de usuario

Herramientas del sitio


wiki2:python:django:models

Diferencias

Muestra las diferencias entre dos versiones de la página.

Enlace a la vista de comparación

Ambos lados, revisión anterior Revisión previa
Próxima revisión
Revisión previa
wiki2:python:django:models [2018/10/19 14:42]
alfred [Get an app model]
wiki2:python:django:models [2020/05/09 09:25] (actual)
Línea 1: Línea 1:
 ====== Django Models ====== ====== Django Models ======
 +===== Migrations =====
 +
 +<​code>​
 +$ python manage.py makemigrations <app>
 +</​code> ​
 +Will create migration files for all the apps if the ''​app''​ was not indicated.
 +
 +<​code>​
 +$ python manage.py migrate
 +</​code> ​
 +Will apply migrations to the configured DB.
 ===== Signals ===== ===== Signals =====
  
Línea 60: Línea 71:
  
 ===== Fields ===== ===== Fields =====
 +==== DateTime Field ====
 +The auto_now_add will set the timezone.now() only when the instance is created, and auto_now will update the field everytime the save method is called.
  
 +<code python>
 +class Invoice(models.Model):​
 +    description = models.CharField(max_length=255)
 +    status = models.CharField(max_length=10)
 +    vendor = models.ForeignKey(Vendor)
 +    created_at = models.DateTimeField(auto_now_add=True)
 +    updated_at = models.DateTimeField(auto_now=True)
 +</​code>​
 ==== Image Field ==== ==== Image Field ====
  
Línea 89: Línea 110:
 </​code>​ </​code>​
  
 +
 +==== Choice ====
 +<​code>​
 +from django.db import models
 +
 +class Person(models.Model):​
 +    SHIRT_SIZES = (
 +        ('​S',​ '​Small'​),​
 +        ('​M',​ '​Medium'​),​
 +        ('​L',​ '​Large'​),​
 +    )
 +    name = models.CharField(max_length=60)
 +    shirt_size = models.CharField(max_length=2,​ choices=SHIRT_SIZES)
 +</​code>​
 +Get its values
 +<​code>​
 +>>>​ p = Person(name="​Fred Flintstone",​ shirt_size="​L"​)
 +>>>​ p.save()
 +>>>​ p.shirt_size
 +'​L'​
 +>>>​ p.get_shirt_size_display()
 +'​Large'​
 +</​code>​
 ==== Special fields ==== ==== Special fields ====
  
Línea 114: Línea 158:
  
 ===== How to... ===== ===== How to... =====
 +==== Deal with performed queries ====
 +See the current registered queries (with their times):
 +<​code>​
 +from django.db import connection
 +connection.queries
 +</​code>​
 +
 +See a concrete database queries:
 +<​code>​
 +from django.db import connections
 +connections['​my_db_alias'​].queries
 +</​code>​
 +
 +Delete the query array:
 +<​code>​
 +from django.db import reset_queries
 +reset_queries()
 +</​code>​
 ==== Get an app model ==== ==== Get an app model ====
 <​code>​ <​code>​
Línea 128: Línea 190:
            ​creator=User.objects.order_by('?'​).first())            ​creator=User.objects.order_by('?'​).first())
 reg.save() reg.save()
 +</​code>​
 +
 +==== Clean migrations ====
 +<​code>​
 +find . -path "​*/​migrations/​*.py"​ -not -name "​__init__.py"​ -delete
 +find . -path "​*/​migrations/​*.pyc" ​ -delete
 +</​code>​
 +
 +=== Script to reset the whole DB ===
 +<​code>​
 +#!/bin/sh
 +
 +export PGPASSWORD=mysecretpassword
 +export DATABASE=puma
 +
 +find . -path "​*/​migrations/​*.py"​ -not -name "​__init__.py"​ -delete
 +find . -path "​*/​migrations/​*.pyc" ​ -delete
 +
 +dropdb -h 127.0.0.1 -U postgres $DATABASE
 +createdb -T template0 -h 127.0.0.1 -U postgres $DATABASE
 +
 +python manage.py makemigrations
 +python manage.py migrate
 +
 +psql -h 127.0.0.1 -U postgres -d $DATABASE -c "​INSERT INTO public.pumapp_user (password,​last_login,​is_superuser,​username,​first_name,​last_name,​is_staff,​is_active,​date_joined,​email,​current_project_id) VALUES ('​pbkdf2_sha256$100000$8RoYX0VMbGIY$OZrZowL8A4pR/​wgE/​ggy7KOSAU82068MEG3tiZHV70I=',​NULL,​true,'​admin','','',​true,​true,'​2019-01-31 08:​22:​29.609','​admin@a.com',​NULL);"​
 +psql -h 127.0.0.1 -U postgres -d $DATABASE -c "​INSERT INTO public.redmine_auth_redmineconfig (url) VALUES ('​https://​redmine.gtd.es'​);"​
 </​code>​ </​code>​
wiki2/python/django/models.1539960166.txt.gz · Última modificación: 2020/05/09 09:24 (editor externo)