Я делаю большой беспорядок, пытаясь получить доступ к объекту, который я передал из представления в форму.
class PrenotaForm(forms.ModelForm):
ORARI_CHOICES = ()
def __init__(self, *args, **kwargs):
DICT_ORARI_CHOICES = kwargs.pop('ORARI_CHOICES_NEW', {})
ORARI_CHOICES_NEW = []
for key, value in DICT_ORARI_CHOICES.items():
temp = [key,value]
ORARI_CHOICES_NEW.append(temp)
super(PrenotaForm, self).__init__(*args, **kwargs)
self.ORARI_CHOICES = ORARI_CHOICES_NEW
print("EEEEEEEEEEEEEEE" + str(self.ORARI_CHOICES))
print(ORARI_CHOICES)
Я не понимаю, почему внутри init заполняется ORARI_CHOICES, как показано в выводе консоли:
EEEEEEEEEEEEEEE [['è uguale', 'Indifferente'], ['845', '08: 45 '], [' 900 ', '09: 00'], ['915', {'label': '09: 15 ',' отключено ':' отключено '}], [' 930 ', {' label ': '09: 30', 'отключено': 'отключено'}], ['945', '09: 45 '], [' 1000 ', '10: 00'], ['1015', '10: 15 '], [' 1030 ', '10: 30'], ['1045', '10: 45 ' ], ['1100', '11: 00 '], [' 1115 ', '11: 15'], ['1130', '11: 30 '], [' 1145 ', '11: 45']]
но вне инициализации ORARI_CHOICE все еще пуст:
print(ORARI_CHOICES)
так как печать ничего не выводит.
Как я могу переопределить ORARI_CHOICES = ()
и сделать его доступным глобально в классе после каждого запроса GET, выполненного в представлении?
if request.method == 'GET':
size_gruppi = 30
print("gruppi size is : " + str(size_gruppi))
ORARI_CHOICES = (
('è uguale', "Indifferente"),
('845', "08:45"),
('900', "09:00"),
('915', "09:15"),
('930', "09:30"),
('945', "09:45"),
('1000', "10:00"),
('1015', "10:15"),
('1030', "10:30"),
('1045', "10:45"),
('1100', "11:00"),
('1115', "11:15"),
('1130', "11:30"),
('1145', "11:45"),
)
orari_map = map(list,ORARI_CHOICES)
orari_dict = dict(ORARI_CHOICES)
print(orari_dict)
counter = 0
for key in orari_map:
if key[0] != 'è uguale':
tot_in_fascia = sum(filter(None, Iscritto.objects.filter(fasce_orarie=key[0]).aggregate(Sum('size_adulti'), Sum('size_giovani')).values()))
print(tot_in_fascia)
if tot_in_fascia >= size_gruppi:
print("fascia " + key[0] + " è al completo ")
orari_dict.update({key[0]: {'label': key[1], 'disabled': 'disabled'}})
form = PrenotaForm(ORARI_CHOICES_NEW = orari_dict)
return render(request, "prenota.html", {'form': form, 'posti_liberi': posti_disponibili, 'giovani_iscritti': giovani_iscritti})