[Answered ]-How to retrieve string data from many to many form field in django

1👍

You write this statement:

all_weekdays[0] != current_end_day or all_weekdays[1] != current_end_day or all_weekdays[2] != current_end_day or all_weekdays[3] != current_end_day or all_weekdays[4] != current_end_day

What do you suppose is the truth value of this statement? Let’s say that all_weekdays is a list from Monday to Friday and current_end_day is Wednesday. This becomes:

"Monday" != "Wednesday" or "Tuesday" != "Wednesday" or ...

Notice the problem yet? Well current_end_day is not Monday so you can rewrite the statement as:

True or "Tuesday" != "Wednesday" or ...

And what is the truth value of True or statement? It is True of course so you are going into an infinite loop.

Also current_end_day is simply a string while all_weekdays is a QuerySet of Weekday objects hence your comparison would always fail. You should firsty get the corresponding Weekday object and use that to compare or as your own comment shows filter on the QuerySet all_weekdays to check if current_end_day exists in it:

elif not all_weekdays.filter(weekday=current_end_day).exists():
    days = days + 1

Leave a comment