dead_count -1 if instance for count dead in enumerate instance deadlin

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
dead_count = -1
if instance:
for count, dead in enumerate(instance.deadlines.filter(\
type=DeadLine.TYPES.PHOTO).order_by('time')):
field = forms.DateTimeField(label=u'Отчёт №%d' % (count + 1), required=False, widget=SelectDateWidget(box_class='date_%d photo_deadline' % (count)))
name = 'photo_deadline_%d' % count
initial[name] = dead.time
self.base_fields[name] = field
dead_count += 1
super(TaskForm, self).__init__(*args, **kwargs)
# Misterious code: newforms are caching fields o_O
# And I'm killing caching fields..
# David Blane, stop, stop stop it!!!!
if instance:
for key in self.fields.keys():
if key.startswith('photo_deadline_'):
count = int(key[len('photo_deadline_'):])
if count > dead_count:
del self.fields[key]