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 StudentAnswersAdmin(admin.ModelAdmin): | |
def get_readonly_fields(self, request, obj=None): | |
# make all fields readonly | |
readonly_fields = list(set( | |
[field.name for field in self.opts.local_fields] + | |
[field.name for field in self.opts.local_many_to_many] | |
)) | |
if 'is_submitted' in readonly_fields: | |
readonly_fields.remove('is_submitted') |
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 | |
import re | |
import sys | |
import argparse | |
renaming_enabled = False | |
dir_path = os.path.dirname(os.path.realpath(__file__)) | |
def rename_file(file_name, new_file_name): |
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
#! /usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import datetime | |
from mock import patch | |
from django.test import TestCase | |
from django.utils import timezone | |
class DatesTestCase(TestCase): |
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 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 |
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.contrib import admin | |
from product.forms import EquipmentForm | |
@admin.register(Equipment) | |
class EquipmentAdmin(admin.ModelAdmin): | |
form = EquipmentForm | |
inlines = [ | |
EquipmentGalleryInline, |
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 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 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 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 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.util import ErrorList | |
from django import forms | |
class ContractUpdateView(UpdateView): | |
model = Contract | |
template_name = 'contract/contract_form.html' | |
form_class = ContractForm | |
def form_valid(self, form): | |
if self.request.POST.get('finish'): |
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 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 |
NewerOlder