This file contains hidden or 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.core.validators import RegexValidator | |
plates_validator = RegexValidator( | |
r'^([A-Z]{3}\d{3}|[A-Z]{2}\d{4}|[A-Z]{2}\d{3}[A-Z]|[A-Z]\d{4,5}|[A-Z]{3}\d{2}[A-Z])$', | |
u'Combinaciones posibles: ABC123, AB1234, AB123C, A1234, A12345, ABC12D' | |
) | |
# http://www.regexr.com/391ai |
This file contains hidden or 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 MyForm(forms.ModelForm): | |
requested_asset = None | |
def __init__(self, *args, **kwargs): | |
other_variable = kwargs.pop('other_variable') | |
super(MyForm, self).__init__(*args, **kwargs) |
This file contains hidden or 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
# source: http://stackoverflow.com/questions/1317714/how-can-i-filter-a-date-of-a-datetimefield-in-django | |
from django.utils import timezone | |
today_min = datetime.datetime.combine(timezone.now().date(), datetime.time.min) | |
today_max = datetime.datetime.combine(timezone.now().date(), datetime.time.max) | |
MyModel.objects.get(date__range=(today_min, today_max)) |
This file contains hidden or 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 customer.models import Customer | |
class AssertRequestForm(forms.ModelForm): | |
customer = forms.ModelChoiceField( | |
label=u'Cliente', | |
queryset=Customer.objects.all(), | |
required=False, | |
) |
This file contains hidden or 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
#! /usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from userprofile.models import User | |
class EmailModelBackend(object): | |
""" Autenticar el usuario con email """ | |
def authenticate(self, username=None, password=None): | |
kwargs = {'email': username} |
This file contains hidden or 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 MyAdmin(admin.ModelAdmin): | |
def has_delete_permission(self, request, obj=None): | |
return False | |
def get_actions(self, request): | |
actions = super(MyAdmin, self).get_actions(request) | |
if 'delete_selected' in actions: | |
del actions['delete_selected'] | |
return actions |
This file contains hidden or 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 json | |
from django.test import TestCase | |
class JSONViewTestCase(TestCase): | |
def test_json_view(self): | |
response = self.client.post( | |
reverse('my_json_view'), | |
json.dumps({ | |
# your JSON |
This file contains hidden or 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 BasicAdmin(admin.ModelAdmin): | |
# ... | |
def get_urls(self): | |
urls = super(BasicAdmin, self).get_urls() | |
my_urls = patterns( | |
'', | |
(r'^download_as_xls/$', self.download_as_xls_view) | |
) | |
return my_urls + urls |
This file contains hidden or 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.forms.models import model_to_dict | |
instance = MyModel.objects.first() | |
fields = model_to_dict(instance).keys() |
This file contains hidden or 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 ContactForm(forms.ModelForm): | |
def __init__(self, *args, **kwargs): | |
super(ContactForm, self).__init__(*args, **kwargs) | |
self.fields['topic'].queryset = ContactTopic.objects.filter( | |
is_active=True, | |
) | |
class Meta: | |
model = ContactMessage |