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 [2019/02/09 11:24]
alfred [Fields]
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 99: 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 124: 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>​
wiki2/python/django/models.1549711473.txt.gz · Última modificación: 2020/05/09 09:24 (editor externo)