You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

42 lines
1.1 KiB

# -*- coding: utf-8 -*-
from django import forms
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
class AdminForm(forms.Form):
model = None
def __init__(self, *args, **kwargs):
"""
create dynamical translated fields fields
"""
if len(settings.LANGUAGES) in range(10):
for lid, (code, name) in enumerate(settings.LANGUAGES):
# uses enumerate for detect iteration number
# first iteration is a default lang so it required fields
required = True if lid == 0 else False
class AdminFilterForm(forms.Form):
"""
class for filtering lists in admin panel
"""
model = None # which models work with
name = forms.CharField(label=_(u'Имя'), required=False)
def filter(self):
"""
return filtered queryset
form must be cleaned before calling this method
"""
data = self.cleaned_data
name = data['name']
model = self.model
qs = model.objects.all()
if name:
qs = qs.filter(translations__name__contains=name)
return qs