This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import forms | |
from fields import DecimalInput | |
class ExampleForm(forms.Form): | |
meu_campo = DeicmalInput() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def GetAdminAddURL(self, model_class=None, model_instance=None): | |
# Se passar a classe, utiliza a classe, se passar a instancia | |
# pega a classe dela. | |
if model_class is None: | |
model_class = model_instance.__class__ | |
from django.core import urlresolvers | |
from django.contrib.contenttypes.models import ContentType | |
content_type = ContentType.objects.get_for_model(model_class) | |
return urlresolvers.reverse("admin:%s_%s_add" % (content_type.app_label, | |
content_type.model)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.views.generic.simple import direct_to_template | |
from app1.models import Model1 | |
from app2.models import Model2 | |
def index(request): | |
model1_objects = Model1.objects.all()[:3] | |
try: | |
model2_destaque = Model2.objects.all()[:1][0] | |
except IndexError: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import forms | |
class Form1(forms.Form): | |
arquivo = forms.FileField() | |
def ler_conteudo_do_arquivo(self): | |
return self.cleaned_data['arquivo'].read() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import connection | |
from agenda.models import Evento | |
Evento.objects.extra( | |
select={ | |
'month': connection.ops.date_extract_sql('month', 'data'), | |
'year': connection.ops.date_extract_sql('year', 'data'), | |
}).values('month', 'year').distinct() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% for evento in eventos %} | |
{% ifchanged %} | |
<h3>{{ evento.data|date:"%F/%Y" }}</h3> | |
{% endifchanged %} | |
{{ evento }} | |
{% endfor %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_first_object_or_none(queryset): | |
try: | |
return queryset[:1][0] | |
except IndexError: | |
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, uuid | |
from django.conf import settings | |
from django.db import models | |
from django.utils.encoding import smart_unicode, smart_str | |
class BannerFileField(models.FileField): | |
def pre_save(self, model_instance, add): | |
file = super(BannerFileField, self).pre_save(model_instance, add) | |
if not add: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ProjectForm(ModelForm) | |
.... | |
def clean_name(self): | |
user = self.cleaned_data['user'] | |
queryset = User.objecs.filter(user.id, active=True) | |
if self.instance and self.instance.user: | |
queryset = queryset.exclude(pk = self.instance.user.pk) | |
if queryset.count(): | |
raise forms.ValidationError(_("This man has been already assign to a partner")) | |
return user |
OlderNewer