Skip to content

Commit 0eee471

Browse files
committed
add typedchoicefield django examples
1 parent d8bea0e commit 0eee471

File tree

2 files changed

+172
-1
lines changed

2 files changed

+172
-1
lines changed
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
title: django.forms TypedChoiceField Python Code Examples
2+
category: page
3+
slug: django-forms-typedchoicefield-examples
4+
sortorder: 500013125
5+
toc: False
6+
sidebartitle: django.forms TypedChoiceField
7+
meta: View code examples that show how to use the TypedChoiceField class within the forms module of Django.
8+
9+
10+
[TypedChoiceField](https://github.com/django/django/blob/master/django/forms/fields.py)
11+
([documentation](https://docs.djangoproject.com/en/stable/ref/forms/fields/#typedchoicefield)),
12+
from the [Django](/django.html) `forms` module, enables safe handling of
13+
pre-defined selections collected via an HTTP POST request from an
14+
[HTML](/hypertext-markup-language-html.html) form submission.
15+
16+
TypedChoiceField can either be imported from `django.forms` or
17+
`django.forms.fields`. `django.forms` is more commonly used because it
18+
is less characters for the equivalent effect.
19+
20+
21+
## Example 1 from dmd-interpreter
22+
[dmd-interpreter](https://github.com/mitchalexbailey/dmd-interpreter)
23+
([running web app](http://www.dmd.nl/DOVE))
24+
is a Python tool to aggregate clinically relevant information related
25+
to variants in the DMD gene and display that [data](/data.html) to a user
26+
with a [Django](/django.html) web application.
27+
28+
[**dmd-interpreter / interpreter / forms.py**](https://github.com/mitchalexbailey/dmd-interpreter/blob/master/interpreter/./forms.py)
29+
30+
```python
31+
# forms.py
32+
~~from django import forms
33+
34+
choices = [(True,'Yes'),(False,'No')]
35+
36+
class IndexForm(forms.Form):
37+
mutation = forms.CharField(label = 'Mutation', max_length = 100)
38+
39+
class ACMGForm(forms.Form):
40+
~~ pvs1 = forms.TypedChoiceField(label = 'Does the variant cause a premature stop codon (nonsense)?', choices=choices, widget=forms.RadioSelect)
41+
~~ pvs2 = forms.TypedChoiceField(label = 'Does the variant cause a frameshift?', choices=choices, widget=forms.RadioSelect)
42+
~~ pvs3 = forms.TypedChoiceField(label = 'Does the variant cause a multiexon deletion involving key functional domains?', choices=choices, widget=forms.RadioSelect)
43+
~~ pvs4 = forms.TypedChoiceField(label = 'Does the variant cause a change in splice site?', choices=choices, widget=forms.RadioSelect)
44+
~~ pvs5 = forms.TypedChoiceField(label = 'Does the variant cause a change in initiation codon?', choices=choices, widget=forms.RadioSelect)
45+
~~ ps1 = forms.TypedChoiceField(label = 'Is there a reported pathogenic variant causing the same amino acid change?', choices=choices, widget=forms.RadioSelect)
46+
~~ ps2 = forms.TypedChoiceField(label = 'Is the variant <i>de novo</i> (confirmed to not be present in either parent)?', choices=choices, widget=forms.RadioSelect)
47+
~~ ps3 = forms.TypedChoiceField(label = 'Are there well-established <i>in vitro</i> studies predicting a damaging effect of this variant on the gene or gene product?', choices=choices, widget=forms.RadioSelect)
48+
~~ ps4 = forms.TypedChoiceField(label = 'Is this variant more prevalence in affected individuals versus controls? (OR > 5.0)', choices=choices, widget=forms.RadioSelect)
49+
~~ pm1 = forms.TypedChoiceField(label = 'Is this variant located in a mutational hot spot and/or critical functional domain?', choices=choices, widget=forms.RadioSelect)
50+
~~ pm2 = forms.TypedChoiceField(label = 'Is the variant absent from controls (autosomal dominant), or found at an extremely low frequency (autosomal recessive)? (Ex. in ExAC or 1000 genomes)', choices=choices, widget=forms.RadioSelect)
51+
~~ pm3 = forms.TypedChoiceField(label = 'Is this variant in a gene linked to an autosomal recessive condition and in <i>trans</i> with a pathogenic variant?', choices=choices, widget=forms.RadioSelect)
52+
~~ pm4 = forms.TypedChoiceField(label = 'Does the variant change the protein length (while preserving reading frame; deletion, insertion, stop-loss)?', choices=choices, widget=forms.RadioSelect)
53+
~~ pm5 = forms.TypedChoiceField(label = 'Does the variant cause a missense change at a residue where a different change is known to be pathogenic?', choices=choices, widget=forms.RadioSelect)
54+
~~ pm6 = forms.TypedChoiceField(label = 'Do you think the variant is <i>de novo</i> but there has not been confirmation (sequencing of parents)?', choices=choices, widget=forms.RadioSelect)
55+
~~ pp1 = forms.TypedChoiceField(label = 'Is the variant in a known disease-causing gene and has it co-segregated with affected family members?', choices=choices, widget=forms.RadioSelect)
56+
~~ pp2 = forms.TypedChoiceField(label = 'Is the variant in a gene where disease-causing variants are not commonly missense, and in which missense variants are a common mechanism of disease?', choices=choices, widget=forms.RadioSelect)
57+
~~ pp3 = forms.TypedChoiceField(label = 'Do multiple <i>in silico</i> functional predication tools support a deleterious effect on the gene or gene product?', choices=choices, widget=forms.RadioSelect)
58+
~~ pp4 = forms.TypedChoiceField(label = 'Does the patient\'s phenotype and/or family history strongly indicate a disease with a single genetic ontology?', choices=choices, widget=forms.RadioSelect)
59+
~~ pp5 = forms.TypedChoiceField(label = 'Does a reputable source report the variant as pathogenic (but the evidence is not available)?', choices=choices, widget=forms.RadioSelect)
60+
```
61+
62+
63+
# Example 2 from django-angular
64+
[django-angular](https://github.com/jrief/django-angular)
65+
([project examples website](https://django-angular.awesto.com/classic_form/))
66+
is a library with helper code to make it easier to use
67+
[Angular](/angular.html) as the front-end to [Django](/django.html) projects.
68+
The code for django-angular is
69+
[open source under the MIT license](https://github.com/jrief/django-angular/blob/master/LICENSE.txt).
70+
71+
[**django-angular / djng / forms / fields.py**](https://github.com/jrief/django-angular/blob/master/djng/forms/fields.py)
72+
73+
```python
74+
# -*- coding: utf-8 -*-
75+
from __future__ import unicode_literals
76+
77+
import re
78+
import mimetypes
79+
80+
from django.conf import settings
81+
from django.contrib.staticfiles.storage import staticfiles_storage
82+
from django.core import signing
83+
from django.core.exceptions import ImproperlyConfigured, ValidationError
84+
from django.core.files.storage import default_storage
85+
from django.core.files.uploadedfile import InMemoryUploadedFile, TemporaryUploadedFile
86+
from django.urls import reverse_lazy
87+
~~from django.forms import fields, models as model_fields, widgets
88+
from django.utils.html import format_html
89+
from django.utils.module_loading import import_string
90+
from django.utils.safestring import mark_safe
91+
from django.utils.translation import ugettext_lazy as _, ungettext_lazy
92+
93+
from djng import app_settings
94+
from .widgets import DropFileWidget, DropImageWidget
95+
96+
97+
## ... source file abbreviated to get to the TypedChoiceField examples ...
98+
99+
~~class TypedChoiceField(MultipleFieldMixin, fields.TypedChoiceField):
100+
~~ def get_potential_errors(self):
101+
~~ if isinstance(self.widget, widgets.RadioSelect):
102+
~~ errors = self.get_multiple_choices_required()
103+
~~ else:
104+
~~ errors = self.get_input_required_errors()
105+
~~ return errors
106+
107+
108+
109+
## ... source file continues with no further examples ...
110+
```
111+
112+
113+
## Example 3 from django-filter
114+
[django-filter](https://github.com/carltongibson/django-filter)
115+
([project documentation](https://django-filter.readthedocs.io/en/master/)
116+
and
117+
[PyPI page](https://pypi.org/project/django-filter/2.2.0/))
118+
makes it easier to filter down querysets from the
119+
[Django ORM](/django-orm.html) by providing common bits of boilerplate
120+
code. django-filter is provided as
121+
[open source](https://github.com/carltongibson/django-filter/blob/master/LICENSE).
122+
123+
[**django-filter / django_filters / filters.py**](https://github.com/carltongibson/django-filter/blob/master/django_filters/./filters.py)
124+
125+
```python
126+
from collections import OrderedDict
127+
from datetime import timedelta
128+
129+
~~from django import forms
130+
from django.db.models import Q
131+
from django.db.models.constants import LOOKUP_SEP
132+
from django.forms.utils import pretty_name
133+
from django.utils.itercompat import is_iterable
134+
from django.utils.timezone import now
135+
from django.utils.translation import gettext_lazy as _
136+
137+
from .conf import settings
138+
from .constants import EMPTY_VALUES
139+
from .fields import (
140+
BaseCSVField,
141+
BaseRangeField,
142+
ChoiceField,
143+
DateRangeField,
144+
DateTimeRangeField,
145+
IsoDateTimeField,
146+
IsoDateTimeRangeField,
147+
LookupChoiceField,
148+
ModelChoiceField,
149+
ModelMultipleChoiceField,
150+
MultipleChoiceField,
151+
RangeField,
152+
TimeRangeField
153+
)
154+
from .utils import get_model_field, label_for_filter
155+
156+
157+
## ... source file abbreviated to get to code examples ...
158+
159+
160+
~~class TypedChoiceFilter(Filter):
161+
~~ field_class = forms.TypedChoiceField
162+
163+
164+
class UUIDFilter(Filter):
165+
field_class = forms.UUIDField
166+
167+
168+
## ... source file continues with no further TypedChoiceField examples ...
169+
170+
```
171+

theme/templates/code-examples/django.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ <h4 class="bp"><a href="/django-forms-examples.html">django.forms</a>
4646
<a href="/django-forms-booleanfield-examples.html">BooleanField</a>,
4747
<a href="/django-forms-charfield-examples.html">CharField</a>,
4848
<a href="/django-forms-choicefield-examples.html">ChoiceField</a>,
49-
<span class="sn">TypedChoiceField</span>,
49+
<a href="/django-forms-typedchoicefield-examples.html">TypedChoiceField</a>,
5050
<span class="sn">DateField</span>,
5151
<span class="sn">DateTimeField</span>,
5252
<span class="sn">DecimalField</span>,

0 commit comments

Comments
 (0)