More Related Content Similar to Lo nuevo de Django 1.7 y 1.8 (20) Lo nuevo de Django 1.7 y 1.89. AppConfig
from django.apps import AppConfig
class RockNRollConfig(AppConfig):
name = 'rock_n_roll'
verbose_name = "Rock ’n’ roll”
class GypsyJazzConfig(RockNRollConfig):
verbose_name = "Gypsy jazz“
INSTALLED_APPS = [
'anthology.apps.GypsyJazzConfig',
# ...
]
11. Forms add error
def clean(self):
cleaned_data = super(ContactForm, self).clean()
cc_myself = cleaned_data.get("cc_myself")
subject = cleaned_data.get("subject")
if cc_myself and subject and "help" not in subject:
msg = u"Must put 'help' in subject."
self.add_error('cc_myself', msg)
self.add_error('subject', msg)
13. Queryset as managers
class MyQueryset(models.QuerySet):
def published(self):
return self.filter(published=True)
class MyModel(models.Model):
objects = MyQueryset.as_manager()
...
17. Migrations directory
$ ./manage.py startapp myapp
$ tree
.
|-- djangounchained
| |-- settings.py
| |-- urls.py
| `-- wsgi.py
|-- manage.py
`-- myapp
|-- admin.py
|-- migrations
|-- models.py
`-- views.py
20. The Migration File
class Migration(migrations.Migration):
dependencies = []
operations = [
migrations.CreateModel(
name='Entry',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False,
auto_created=True, primary_key=True)),
('title', models.CharField(max_length=200)),
('content', models.TextField()),
('slug', models.SlugField(unique=True, max_length=200)),
],
options={
},
bases=(models.Model,),
),
]
23. CreateModel
migrations.CreateModel(
name='Person',
fields=[
('id', models.AutoField(verbose_name='ID',
serialize=False, auto_created=True,
primary_key=True)),
('full_name’,models.CharField(max_length=100)),
],
options={},
bases=(models.Model,),
)
24. AddField
dependencies = [
('people', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='person',
name='first_name',
field=models.CharField(default='',
max_length=50),
preserve_default=False,
),
25. RunPython
def split_names(apps, schema_editor):
Person = apps.get_model('people.Person')
for person in Person.objects.all():
full_name = person.full_name.split(' ')
person.first_name = full_name[0:-1]
person.last_name = last_name[-1]
person.save()
migrations.RunPython(
split_names,
merge_names
)
31. RangeField
class Event(models.Model):
name = models.CharField(max_length=200)
ages = IntegerRangeField()
>>> Event.objects.create(name='Soft play',
ages=(0, 10))
>>> Event.objects.create(name='Pub trip',
ages=(21, None))
>>> Event.objects.filter(
ages__contains=NumericRange(4, 5))
[<Event: Soft play>]
35. Case, When
>>> Client.objects.annotate(
... discount=Case(
... When(account_type=Client.GOLD,
... then=Value('5%')),
... When(account_type=Client.PLATINUM,
... then=Value('10%')),
... default=Value('0%'),
... output_field=CharField(),
... ),
... ).values_list('name', 'discount')
36. Conditional Update
for cl in Client.objects.all():
if cl.registered <= a_year_ago:
client.account_type = GOLD
elif cl.registerd <= a_month_ago:
client.account_type = PLATINUM
else:
client.account_type = REGULAR
client.save()
39. TEMPLATES setting
TEMPLATES = [
{
'BACKEND':
'django.template.backends.django.DjangoTemplates',
#'django.template.backends.jinja2.Jinja2'
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {},
},
]
40. Multiple Template Engines
TEMPLATES = [
{
’NAME': 'django'
'BACKEND':
'django.template.backends.django.DjangoTemplates',
},
{
’NAME': ’jinja2'
'BACKEND':
'django.template.backends.jinja2.Jinja2',
},
]