for key, value in form.cleaned_data.iteritems():Python 3
print key, value
for key, value in form.cleaned_data.items():Reference:
print (key, value)
http://stackoverflow.com/questions/5904969/python-how-to-print-a-dictionarys-key
for key, value in form.cleaned_data.iteritems():Python 3
print key, value
for key, value in form.cleaned_data.items():Reference:
print (key, value)
def get_child_attribute_list(self, element):
return Attribute.objects.filter(Q(element=element), Q(productattribute__product__parent=self)|Q(productattribute__product__root=self))
import sysReference:
import win32_unicode_argv
result_list = sorted(chain(salary_item_list, salary_item_extra_list), key=attrgetter('created'))Deduplicate
unique_results = [rows.next() for (key, rows) in groupby(result_list, key=attrgetter('created')]
Troubleshooting for dj-static (In case static files error)wsgi.py
OSError: [Errno 2] No such file or directory: '/app/PROJECT_NAME/static'
heroku run python manage.py collectstatic --dry-run --noinput
exclusion_list = ['id', 'created']
profile_list = PurchaseProfile.objects.filter(
reference_content_type=ContentType.objects.get_for_model(model), reference_object_id=self.pk)
profile_field_list = profile_list[0]._meta.get_all_field_names()
reference_data = dict([(field.name, getattr(self, field.name)) for field in self._meta.fields if field.name not in exclusion_list and field.name in profile_field_list])
profile_list.update(**reference_data)
response = redirect('salary_payment_list')
response['Location'] += '?type=unpaid'
return response
$("input.calender").each(function(index, element){
if($(element).hasClass("full")){
var id = $(element).attr('id');
$(element).attr('id', id+'_'+index);
$(element).datepicker({
dateFormat: "yy-mm-dd",
changeYear: true,
changeMonth: true
});
}
});
from django.conf import settings
....
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^static_0/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATICFILES_DIRS[0], 'show_indexes': True}),
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
)
....
TEMPLATE_CONTEXT_PROCESSORS = (template.html
....
'django.core.context_processors.request', # not enabled by default
....
)
{{ request.GET.foo }} {{ request.COOKIES }} {{ request.META }} {{ request.user }}# To access current HttpRequest's attributes & methods
pip install virtualenvwrapper-winCMD
mkvirtualenv project_A# Create New Virtualenv for each New Project
workon project_A
key_and_item = dict([(x,0) for x in range(10)])
{0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0}
key_only = list(key_and_item)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# OrderedDict will also have the same effect
1. cmd
python manage.py runserver 8080
2. web browser
http://local.project-a.com:8080
3. cookies check
1. cmd
python manage.py runserver 8888
2. web browser
http://local.project-b.com:8888
3. cookies check
- foreign key & one-to-one relationship
- many-to-many and many-to-one relationshipMore info:
- generic foreign key & relationship
foo = 1Source: http://www.python.org/dev/peps/pep-0308/
A = 'A'
B = 'B'
C = 'C'
x = A if foo > 0 else B
# A
x = lambda: A if foo < 0 else B
x()
# B
x = A if not foo else B if foo < 0 else C
# C
class SalesPerson(User):History - Before Optimized
class Meta:
proxy = True
....
class SalesManager(models.Manager):
def get_sales_person_list(self, user):
if user.is_superuser:
return list(SalesPerson.objects.filter(
pk__in=list(Sales.objects.values_list('sales_person', flat=True)
.order_by("sales_person").distinct())))
class Sales(CommonModel):
....
sales_person = models.ForeignKey(User, related_name="sales_by")
....
objects = SalesManager()
....
return [SalesPerson.objects.get(pk=obj) for obj in self.get_query_set()
.values_list('sales_person', flat=True).order_by("sales_person").distinct()]
INITIAL_CHOICE = ('', '---------')
class ProfileForm(forms.Form):
profile_id = forms.ChoiceField(label=_('Profile'), choices=[INITIAL_CHOICE])
def __init__(self, *args, **kwargs):
super(ProfileForm, self).__init__(*args, **kwargs)
self.fields["profile_id"].choices = [INITIAL_CHOICE]+[(obj.id, "%s [%s]" % (obj.name, obj.identity,)) for obj in Profile.objects.all()]
Sales.objects.extra(select={
'grand_total': "cost_foo_1 + cost_foo_2 + cost_foo_3"
}, where=['id=%s'], params=[self.pk])[0].grand_total