5 examples of 'django choices' in Python

Every line of 'django choices' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
150def __init__(self, value=None, label=None):
151 Choice.order += 1
152 self.value = value
153 self.label = label
154 self.order = Choice.order
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

407def get_choices(self):
408 """
409 Returns django-compatible choices
410 """
411 c = connection.cursor()
412 c.execute(
413 """
414 SELECT DISTINCT \"%(col)s\"
415 FROM %(table)s
416 WHERE \"%(col)s\" IS NOT NULL AND \"%(col)s\" != ''
417 ORDER BY \"%(col)s\""""
418 % {"col": self.db_column, "table": self.table}
419 )
420 return [(x, x) for x, in c.fetchall()]
176@property
177def choices(self):
178 if self._choices is None:
179 self._choices = list(self._choice_set.all().order_by("position"))
180 return self._choices
60def __init__(self):
61 values = []
62
63 for k, v in self.__class__.__dict__.items():
64 if type(v) == Choice:
65 values.append(v)
66
67 if not values:
68 raise ValueError("Choices class declared with no actual choice fields.")
69
70 values.sort(lambda x, y: x.global_id - y.global_id)
71
72 last_choice_id = 0
73 for choice in values:
74 if choice.id == -255:
75 last_choice_id += 1
76 choice.id = last_choice_id
77 last_choice_id = choice.id
78 self.append((choice.id, choice.desc))
22def _set_choices(self, value):
23 # we overwrite this function so no list(value) is called
24 self._choices = self.widget.choices = value

Related snippets