Django - Altering a Form field into a choices field and populating dynamically
In my models I have a char field as per the below:
alerting_tier = models.CharField(max_length=200, blank=True, null=True)
However in my form I'm pulling external data that I would like to use a choices for this field. My attempt thus far is not showing errors but is not showing a choice field either, its just a textbook.
class DeviceForm(forms.ModelForm):
class Meta:
model = Device
fields = ['site', 'switches', 'hostname', 'serial_no','version', 'template', 'model', 'install_date',
'ospf_area','snmp_data','alerting_tier','host_data','solarwinds_id','smartnet','bau',
'smartnet_contract_id','smartnet_contract_start_date','smartnet_contract_end_date','alerting_tier']
def __init__(self, *args, **kwargs):
site_id = kwargs.pop('site_id', None)
device_id = kwargs.pop('device_id', None)
self.is_add = kwargs.pop("is_add", False)
self.blank_site = kwargs.pop("blank_site", False)
super(DeviceForm, self).__init__(*args, **kwargs)
# get the alerting tiers from solarwinds
swis = solarwinds_conn()
tier_query = """
SELECT
c.Field,
c.Value,
c.DisplayName
FROM
Orion.CustomPropertyValues c
WHERE
Field = 'AlertingTier'
"""
tier_results = swis.query(tier_query)
tiers = tier_results["results"]
tier_options =
for i in tiers:
tier_options.append(i['Value'])
self.fields['alerting_tier'].choices = tier_options
self.fields['alerting_tier'].widget.choices = tier_options
EDIT:
tried the below:
self.fields['alerting_tier'] = forms.Select()
self.fields['alerting_tier'].choices = tier_options
returning error:
'Select' object has no attribute 'get_bound_field'
python django django-forms
add a comment |
In my models I have a char field as per the below:
alerting_tier = models.CharField(max_length=200, blank=True, null=True)
However in my form I'm pulling external data that I would like to use a choices for this field. My attempt thus far is not showing errors but is not showing a choice field either, its just a textbook.
class DeviceForm(forms.ModelForm):
class Meta:
model = Device
fields = ['site', 'switches', 'hostname', 'serial_no','version', 'template', 'model', 'install_date',
'ospf_area','snmp_data','alerting_tier','host_data','solarwinds_id','smartnet','bau',
'smartnet_contract_id','smartnet_contract_start_date','smartnet_contract_end_date','alerting_tier']
def __init__(self, *args, **kwargs):
site_id = kwargs.pop('site_id', None)
device_id = kwargs.pop('device_id', None)
self.is_add = kwargs.pop("is_add", False)
self.blank_site = kwargs.pop("blank_site", False)
super(DeviceForm, self).__init__(*args, **kwargs)
# get the alerting tiers from solarwinds
swis = solarwinds_conn()
tier_query = """
SELECT
c.Field,
c.Value,
c.DisplayName
FROM
Orion.CustomPropertyValues c
WHERE
Field = 'AlertingTier'
"""
tier_results = swis.query(tier_query)
tiers = tier_results["results"]
tier_options =
for i in tiers:
tier_options.append(i['Value'])
self.fields['alerting_tier'].choices = tier_options
self.fields['alerting_tier'].widget.choices = tier_options
EDIT:
tried the below:
self.fields['alerting_tier'] = forms.Select()
self.fields['alerting_tier'].choices = tier_options
returning error:
'Select' object has no attribute 'get_bound_field'
python django django-forms
add a comment |
In my models I have a char field as per the below:
alerting_tier = models.CharField(max_length=200, blank=True, null=True)
However in my form I'm pulling external data that I would like to use a choices for this field. My attempt thus far is not showing errors but is not showing a choice field either, its just a textbook.
class DeviceForm(forms.ModelForm):
class Meta:
model = Device
fields = ['site', 'switches', 'hostname', 'serial_no','version', 'template', 'model', 'install_date',
'ospf_area','snmp_data','alerting_tier','host_data','solarwinds_id','smartnet','bau',
'smartnet_contract_id','smartnet_contract_start_date','smartnet_contract_end_date','alerting_tier']
def __init__(self, *args, **kwargs):
site_id = kwargs.pop('site_id', None)
device_id = kwargs.pop('device_id', None)
self.is_add = kwargs.pop("is_add", False)
self.blank_site = kwargs.pop("blank_site", False)
super(DeviceForm, self).__init__(*args, **kwargs)
# get the alerting tiers from solarwinds
swis = solarwinds_conn()
tier_query = """
SELECT
c.Field,
c.Value,
c.DisplayName
FROM
Orion.CustomPropertyValues c
WHERE
Field = 'AlertingTier'
"""
tier_results = swis.query(tier_query)
tiers = tier_results["results"]
tier_options =
for i in tiers:
tier_options.append(i['Value'])
self.fields['alerting_tier'].choices = tier_options
self.fields['alerting_tier'].widget.choices = tier_options
EDIT:
tried the below:
self.fields['alerting_tier'] = forms.Select()
self.fields['alerting_tier'].choices = tier_options
returning error:
'Select' object has no attribute 'get_bound_field'
python django django-forms
In my models I have a char field as per the below:
alerting_tier = models.CharField(max_length=200, blank=True, null=True)
However in my form I'm pulling external data that I would like to use a choices for this field. My attempt thus far is not showing errors but is not showing a choice field either, its just a textbook.
class DeviceForm(forms.ModelForm):
class Meta:
model = Device
fields = ['site', 'switches', 'hostname', 'serial_no','version', 'template', 'model', 'install_date',
'ospf_area','snmp_data','alerting_tier','host_data','solarwinds_id','smartnet','bau',
'smartnet_contract_id','smartnet_contract_start_date','smartnet_contract_end_date','alerting_tier']
def __init__(self, *args, **kwargs):
site_id = kwargs.pop('site_id', None)
device_id = kwargs.pop('device_id', None)
self.is_add = kwargs.pop("is_add", False)
self.blank_site = kwargs.pop("blank_site", False)
super(DeviceForm, self).__init__(*args, **kwargs)
# get the alerting tiers from solarwinds
swis = solarwinds_conn()
tier_query = """
SELECT
c.Field,
c.Value,
c.DisplayName
FROM
Orion.CustomPropertyValues c
WHERE
Field = 'AlertingTier'
"""
tier_results = swis.query(tier_query)
tiers = tier_results["results"]
tier_options =
for i in tiers:
tier_options.append(i['Value'])
self.fields['alerting_tier'].choices = tier_options
self.fields['alerting_tier'].widget.choices = tier_options
EDIT:
tried the below:
self.fields['alerting_tier'] = forms.Select()
self.fields['alerting_tier'].choices = tier_options
returning error:
'Select' object has no attribute 'get_bound_field'
python django django-forms
python django django-forms
edited Nov 15 '18 at 13:00
AlexW
asked Nov 15 '18 at 12:54
AlexWAlexW
45211156
45211156
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You need to set the widget to be forms.Select with the choices on the charfield. A charfield expects you to enter text by default... As you'd expect.
ive added an edit with updated code and am getting a new error
– AlexW
Nov 15 '18 at 13:00
1
self.fields['alerting_tier'].widget = forms.Select()
– Pythonista
Nov 15 '18 at 13:03
You didn't change the widget... You changed the form field.
– Pythonista
Nov 15 '18 at 13:03
sorted! thank you!
– AlexW
Nov 15 '18 at 13:05
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319941%2fdjango-altering-a-form-field-into-a-choices-field-and-populating-dynamically%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need to set the widget to be forms.Select with the choices on the charfield. A charfield expects you to enter text by default... As you'd expect.
ive added an edit with updated code and am getting a new error
– AlexW
Nov 15 '18 at 13:00
1
self.fields['alerting_tier'].widget = forms.Select()
– Pythonista
Nov 15 '18 at 13:03
You didn't change the widget... You changed the form field.
– Pythonista
Nov 15 '18 at 13:03
sorted! thank you!
– AlexW
Nov 15 '18 at 13:05
add a comment |
You need to set the widget to be forms.Select with the choices on the charfield. A charfield expects you to enter text by default... As you'd expect.
ive added an edit with updated code and am getting a new error
– AlexW
Nov 15 '18 at 13:00
1
self.fields['alerting_tier'].widget = forms.Select()
– Pythonista
Nov 15 '18 at 13:03
You didn't change the widget... You changed the form field.
– Pythonista
Nov 15 '18 at 13:03
sorted! thank you!
– AlexW
Nov 15 '18 at 13:05
add a comment |
You need to set the widget to be forms.Select with the choices on the charfield. A charfield expects you to enter text by default... As you'd expect.
You need to set the widget to be forms.Select with the choices on the charfield. A charfield expects you to enter text by default... As you'd expect.
answered Nov 15 '18 at 12:55
PythonistaPythonista
8,72321437
8,72321437
ive added an edit with updated code and am getting a new error
– AlexW
Nov 15 '18 at 13:00
1
self.fields['alerting_tier'].widget = forms.Select()
– Pythonista
Nov 15 '18 at 13:03
You didn't change the widget... You changed the form field.
– Pythonista
Nov 15 '18 at 13:03
sorted! thank you!
– AlexW
Nov 15 '18 at 13:05
add a comment |
ive added an edit with updated code and am getting a new error
– AlexW
Nov 15 '18 at 13:00
1
self.fields['alerting_tier'].widget = forms.Select()
– Pythonista
Nov 15 '18 at 13:03
You didn't change the widget... You changed the form field.
– Pythonista
Nov 15 '18 at 13:03
sorted! thank you!
– AlexW
Nov 15 '18 at 13:05
ive added an edit with updated code and am getting a new error
– AlexW
Nov 15 '18 at 13:00
ive added an edit with updated code and am getting a new error
– AlexW
Nov 15 '18 at 13:00
1
1
self.fields['alerting_tier'].widget = forms.Select()
– Pythonista
Nov 15 '18 at 13:03
self.fields['alerting_tier'].widget = forms.Select()
– Pythonista
Nov 15 '18 at 13:03
You didn't change the widget... You changed the form field.
– Pythonista
Nov 15 '18 at 13:03
You didn't change the widget... You changed the form field.
– Pythonista
Nov 15 '18 at 13:03
sorted! thank you!
– AlexW
Nov 15 '18 at 13:05
sorted! thank you!
– AlexW
Nov 15 '18 at 13:05
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319941%2fdjango-altering-a-form-field-into-a-choices-field-and-populating-dynamically%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown