PollOptions should be replaced with ChoiceField?
as I was looking into ... I had a chance to re-read the non-working code in DjangoLDP-Polls:
@receiver(post_save, sender=Poll)
@on_transaction_commit
def post_create_poll(sender, instance, **kwargs):
# pollOptions list to generate
model = ['Très bien', 'Bien', 'Assez bien', 'Passable', 'Insuffisant', 'À rejeter']
# verify if the options list is already correct
if len(instance.pollOptions.all()) != len(model) or not functools.reduce(lambda x, y : x and y, map(lambda p, q: p.name == q,instance.pollOptions.all(),model), True):
# if not, clear it and create the right ones
instance.pollOptions.clear()
for option in model:
instance.pollOptions.add( PollOption.objects.create(name=option) )
# save the new list (will re-enter this function but the check avoid infinite loop)
instance.save()
The first time I saw this I read "unless other PollOption
s have been defined, then default to these, and I thought it was a nice solution
However if the objective is indeed to hard-code the PollOption
s for every Poll
to a universal 6 options, then I think it would be better to remove the PollOption
model, and replace it with a ChoiceField
It's also possible to provide internationalization on the ChoiceField text
@fabien4vo @balessan have I reflected the spec of DjangoLDP-Polls accurately here?