Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anterior Revisión previa Próxima revisión | Revisión previa | ||
|
wiki2:python:django:models [2019/02/16 20:03] alfred [Create your own field] |
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 145: | Línea 156: | ||
| - | ==== Create your own field ==== | ||
| - | You need the field and the widget: | ||
| - | <code python> | + | ===== How to... ===== |
| - | class SelectMultipleChecks(forms.Widget): | + | ==== Deal with performed queries ==== |
| - | template_name = 'widgets/multiple_checks.html' | + | See the current registered queries (with their times): |
| + | <code> | ||
| + | from django.db import connection | ||
| + | connection.queries | ||
| + | </code> | ||
| - | def __init__(self, *args, choices=(), defaults=(), **kwargs): | + | See a concrete database queries: |
| - | super().__init__(*args, **kwargs) | + | <code> |
| - | self.choices = choices | + | from django.db import connections |
| - | + | connections['my_db_alias'].queries | |
| - | def get_context(self, name, value, attrs): | + | |
| - | context = super().get_context(name, value, attrs) | + | |
| - | context['choices'] = self.choices | + | |
| - | return context | + | |
| - | + | ||
| - | + | ||
| - | class SelectMultipleChecksField(forms.Field): | + | |
| - | widget = SelectMultipleChecks | + | |
| - | + | ||
| - | def __init__(self, *args, choices=(), defaults=(), **kwargs): | + | |
| - | super().__init__(*args, **kwargs) | + | |
| - | self.choices = choices | + | |
| - | + | ||
| - | def _get_choices(self): | + | |
| - | return self._choices | + | |
| - | + | ||
| - | def _set_choices(self, value): | + | |
| - | if callable(value): | + | |
| - | value = CallableChoiceIterator(value) | + | |
| - | else: | + | |
| - | value = list(value) | + | |
| - | self._choices = self.widget.choices = value | + | |
| - | + | ||
| - | choices = property(_get_choices, _set_choices) | + | |
| - | + | ||
| - | def to_python(self, value): | + | |
| - | import json | + | |
| - | values = json.loads(value) | + | |
| - | i_values = [int(v) for v in values] | + | |
| - | return [choice for choice in self._choices if choice.pk in i_values] | + | |
| - | + | ||
| - | def validate(self, value): | + | |
| - | assert len(value) > 0 | + | |
| - | + | ||
| - | def valid_value(self, value): | + | |
| - | return value | + | |
| </code> | </code> | ||
| - | With this we can have the template ''widgets/multiple_checks.html'': | + | Delete the query array: |
| - | <code html> | + | <code> |
| - | <div style="display: flex; justify-content: space-around;"> | + | from django.db import reset_queries |
| - | <input type="hidden" id="{{ widget.attrs.id }}" value="[{{ selected|join:"," }}]"> | + | reset_queries() |
| - | {% for choice in choices %} | + | |
| - | <div><input | + | |
| - | type="checkbox" {% if choice.pk in selected %}checked{% endif %} | + | |
| - | name="{{widget.attrs.id}}_check" | + | |
| - | data-id="{{choice.pk}}" | + | |
| - | > {{ choice }}</div> | + | |
| - | {% endfor %} | + | |
| - | </div> | + | |
| </code> | </code> | ||
| - | ===== How to... ===== | ||
| ==== Get an app model ==== | ==== Get an app model ==== | ||
| <code> | <code> | ||