Getting results of dropdown lists - django

im a beginner in using Django. I need to create a Game for my University project.
I have some different questions, which have always 4 different answers. The answers will once have different values for the player, but im not that far, yet.
I did this in a dropdown list:
class Player(BasePlayer):
Antwort1 = models.IntegerField(
choices=[
[1, 'Mitfahrgelegenheit'],
[2, 'BlaBlaCar'],
[3, 'FlixBus'],
[4, 'Mitfahrzentrale'],
[5, ' ']
], default=5)
Antwort2 = models.IntegerField(
choices=[
[1, 'Mitfahrgelegenheit'],
[2, 'BlaBlaCar'],
[3, 'FlixBus'],
[4, 'Mitfahrzentrale'],
[5, ' ']
], default=5)
Antwort3 = models.IntegerField(
choices=[
[1, 'Mitfahrgelegenheit'],
[2, 'BlaBlaCar'],
[3, 'FlixBus'],
[4, 'Mitfahrzentrale'],
[5, ' ']
], default=5)
and so on...
How can I get the results out of the choices?
If I put {{% form.answer1 %}} in my result page, the result in fact is the answer, but shown as a drop down list.
thanks for your help and best whises,
Julian

Related

Is there a way to permutate list of lists while keeping the lists together?

list1 = [[1,2,3],[4,5,6],[7,8,9]]
Is there anyway possible in the realm of coding that I can permutate this list of lists while keeping the "groups" together?
I really need this solved, been working on a code all week and this is like the last step.
Result should look something like this or close:
permuations_list1 = [[2,3,1],[6,5,4],[8,9,7]], [[3,2,1],[4,5,6],[8,7,9]], .......
I have searched over alot of sites for a solution, but none of them keep the original lists grouped together. Anything helps.
This problem can be split into two steps:
Generate all permutations for each sublist
Generate all unique combinations of (perm_X_of_sublist_1, perm_Y_of_sublist_2, perm_Z_of_sublist_3)
In python, a working solution would look like this:
import itertools
# step 1 - generate all permutations for each sublist
permutated_sublists = (itertools.permutations(sublist) for sublist in list1)
# step 2 - generate a cartesian product of permutated sublists and gather results
permutations_list = list(itertools.product(*permutated_sublists))
The result will look like this
[[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
[[1, 2, 3], [4, 5, 6], [7, 9, 8]],
[[1, 2, 3], [4, 5, 6], [8, 7, 9]],
... (212 more combinations)
[[3, 2, 1], [6, 5, 4], [9, 8, 7]]]

Python: How to encode a solution for an optimization problem

I am working on an optimization problem and needed to encode the solution to the problem. Below is the piece of code I wrote for this task. Part one of the extracts the corresponding cities assigned to each salesman. In Part two of the code, I want to insert the starting and ending depots (cities) of each of the salesmen. I want this process to be dynamic as the starting/ending depots lists will change as the "num_salesmen" variable changes. The "population_list" will hold members of the population. I have given one example to aid in your assistance of this request.
Please let me know if you need further clarification of my logic in the inserting part.
####____BELOW CODE is being designed encode a solution for a GA_____#
populationSize = 1 (this will be varied)
num_salesmen = 2
population_list = [[4, 2, 3], [0, 1, 0], [1, 0], [1, 0]]
## - where [4, 2, 3] is a list of cities to be visited by salesmen,
## - [0, 1, 0] the list of salesman, and
## - [1, 0], [1, 0] are the lists of starting and ending depots of the
salesman one (0) and salesman two (1) respectively.
for pop in population_list:
##----Part ONE: determine cities assigned to each salesman:
Assigned_cites = [[] for x in range(num_salesmen)]
for i in range(len(pop[1])):
for man in range(num_salesmen):
if pop[1][i] == man:
Assigned_cites[man].append(pop[0][i])
##---- Part TWO: inserting the starting and ending depots:
for s_man in range(num_salesmen):
for s_e_d in range(2,num_salesmen+2):
Assigned_cites[s_man].insert(0,pop[s_e_d][0])
Assigned_cites[s_man].append(pop[s_e_d][1])
###- expected result from Part TWO Should look like below, but I am not getting it:
[[1, 4, 3, 0], [1, 2, 0]]
Thanks in advance for your help.
#your extraction logic need a bit of tweaking
Assigned_cites = [[] for x in range(num_salesmen)]
for i in range(len(population_list[1])):
for man in range(num_salesmen):
if population_list[1][i] == man:
Assigned_cites[man].append(population_list[0][i])
print Assigned_cites
s_man = 0 # no need of an outer for loop for sales man
for s_e_d in range(2,num_salesmen+2):
Assigned_cites[s_man].insert(0,population_list[s_e_d][0])
Assigned_cites[s_man].append(population_list[s_e_d][1])
s_man = s_man + 1
print Assigned_cites

Django dynamic verbose name

I'm struggling to think about how to achieve this. What I want to do is have a series of questions (to represent a Likert table) in a CharField object like so:
for a in range(1, 11):
locals()['ATL' + str(a)] = models.PositiveIntegerField(
choices=[
[1, 'Disagree Completely'],
[2, 'Disagree Strongly'],
[3, 'Disagree'],
[4, 'Neutral'],
[5, 'Agree'],
[5, 'Agree Strongly'],
[7, 'Agree Completely'],
],
widget=widgets.RadioSelectHorizontal(),
verbose_name = Constants.ATL_qu_list[a-1])
del a
And then change the verbose name for the question depending on the question number (again, I know I'm not supposed to be using locals() to store variables). Is there an easier way of achieving a dynamic label though? Thanks!
Okay, here's my answer (as well as a clarification for what I am looking for). Basically I had a series of Likert questions to put to participants which I wanted to represent as CharFields. Because each Likert question uses the same seven choice scale, it seems like inefficient coding to repeat the same functionality and only change the verbose name between each declaration.
Accordingly, I've instead used this method to achieve what I want:
# Reads in the list of survey questions
with open('survey/survey_questions.csv') as csvfile:
data_read = list(csv.reader(csvfile))
...
for a in range(1, 11):
locals()['ATL' + str(a)] = models.PositiveIntegerField(
choices=[
[1, 'Disagree Completely'],
[2, 'Disagree Strongly'],
[3, 'Disagree'],
[4, 'Neutral'],
[5, 'Agree'],
[6, 'Agree Strongly'],
[7, 'Agree Completely'],
],
widget=widgets.RadioSelectHorizontal(),
verbose_name = data_read[a-1][0])
del a

Select record from a list of ids through M2M relations

First, sorry for my bad english, this problem is not trivial to explain so I hope you will understand me.
I have 2 models as the following:
class A(models.Model):
code = models.CharField(unique=True, max_length=10)
list_of_b = models.ManyToManyField('B')
class B(models.Model):
code = models.CharField(unique=True, max_length=10)
I aim to retrieve instances of A which match exactly with a given list of B ids.
For example, imagine I have the following records of A in my database:
id: 1 - code: X - list_of_b: [1, 2, 4, 6]
id: 2 - code: Y - list_of_b: [2, 5, 6]
id: 3 - code: Z - list_of_b: [2, 3, 4, 5, 6]
With [2, 5, 6] as given list, I should retrieve the record 2 and 3, not 1.
I succeed to retrieve records with an exact match of ids with this query:
queryset = A.objects.prefetch_related('list_of_b')
queryset = queryset.annotate(nb=Count('list_of_b')).filter(nb=len(my_list))
for id in my_list:
queryset = queryset.filter(list_of_b=id)
It works for the record 2 but not for the record 3.
Thanks for any help. Don't hesitate to question me if not clear enough. ;)
EDIT:
Just one more thing: it's also possible that my_list contains more IDs than necessary. For exemple, with [2, 5, 6, 7] I should retrieve records 2 and 3.
Just remove the filter by count:
queryset = A.objects.prefetch_related('list_of_b')
for id in my_list:
queryset = queryset.filter(list_of_b=id)

Sorted dicts lists manual changing

In my views I have a dict list such as
data = [{'name': 'Abe', 'seq': [4, 3]}, {'name': 'All names', 'seq': [9, 8]}, {'name': 'Mike', 'seq': [11, 2]}]
In my template I sorted them with:
{{ data|dictsort:"name" }}
I'd like to have {'name': 'All names', 'seq': [9, 8]} in the top of the list and keep the other sorted data items. How can I do this?
Do the sorting in the view, using a custom key function.
data.sort(key=lambda d: '0' if d['name'] == 'All names' else d['name'])