Reputation: 2587
I'm struggling with something that seems quite simple to me so I apologize in advance if this seems a silly question. I am struggling to get the value submitted by a form using POST that has values that comes from a drop down list. The values are displayed from the django admin site, ie an admin user can add multiple values in the admin site and these values appear in a drop down list using a MulitpleChoiceField. (this way admins control the ip addresses that can be tested against)
In my models, I have the following configured.
from django.db import models
class ServerIpAddress(models.Model):
name = models.CharField(max_length=200)
def __unicode__(self):
return self.name
from platform.models import ServerIpAddress
In my form, I have the following configured.
serverip = forms.ModelChoiceField(queryset=CloudStackIpAddress.objects.all(),
label='server IP to test')
When the form is submitted, I am collecting the cleaned data in my views.py
cd = form.cleaned_data
serverip = cd['serverip']
However when I review the value comes back as after the form has been submitted I get the following
'serverip': <ServerIpAddress: 10.0.25.14>
I just need the ip address, in this case 10.0.25.14 (or it could be another value). The format I currently have is causing a problem further down the line as the script cannot determine what the IP address is as it currently reads .
Please can someone help?
Upvotes: 0
Views: 72
Reputation: 599460
You have a ServerIpAddress instance - and according to your model, the value is stored in the name
attribute. So if you just need to pass the value to some other function, you would do serverip.name
.
Upvotes: 1
Reputation: 174614
serverip = cd['serverip'].name
should fix your problem.
Did you know that django already provides an IPAddressField
?
Upvotes: 1