1👍
✅
You should not be generating the list leader
in the module. Apart from problems with running migrations, the list will be generated only once when the server starts, and will not be updated when new sponsors are added to the database.
You could wrap the code in a function, for example:
def get_leader():
sponsor_list = Sponsor.objects.all()
leader=[]
for value in sponsor_list:
current_leader = value.name+' '+value.surname+' '+value.middle+' ('+str(value.code_number)+')'
leader.append(current_leader)
return leader
Since you have moved the sponsor_list
variable inside the get_leader
method, you’ll have to update you sponsor
field, e.g change it to:
sponsor = forms.ModelChoiceField(label='Спонсор',queryset=Sponsor.objects.all(), empty_label='---', widget=forms.Select(attrs={'class':'ftrmail'}))
Then change your code that uses leader
to call get_leader()
.
You might need to make further changes – since you haven’t shown where you use the leader
variable it’s not possible to say.
Source:stackexchange.com