Answer the question
In order to leave comments, you need to log in
Convert country_id to country name?
Good day everyone! Please tell me how you can convert country_id back to the name of the country!
The situation is this in the html form, the country field is formed by a widget where the country_id is in value, of course it is written to the extended user model, maybe there is a pill to convert it back into the name of the country? when outputting back to the template
<select class="form-control" id="input-payment-country" name="country_id">
<option value=""> --- Please Select --- </option>
<option value="244">Aaland Islands</option>
<option value="1">Afghanistan</option>
<option value="2">Albania</option>
<option value="3">Algeria</option>
class CreationFormUser(forms.ModelForm):
class Meta:
model = User
fields = ('email', 'last_name', 'first_name', 'address', 'post_code', 'password', 'country_id', 'phone', 'city')
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(_('email address'), unique=True)
first_name = models.CharField(_('first name'), max_length=30, blank=True)
last_name = models.CharField(_('last name'), max_length=30, blank=True)
phone = models.CharField(_('telephone'), max_length=30, blank=True)
country_id = models.CharField(_('country'), max_length=30, blank=True)
city = models.CharField(_('city'), max_length=50, blank=True, default='')
address = models.CharField(_('address'), max_length=30, blank=True)
post_code = models.CharField(_('post code'), max_length=30, blank=True)
date_joined = models.DateTimeField(_('date joined'), auto_now_add=True)
is_active = models.BooleanField(_('active'), default=True)
is_admin = models.BooleanField(_('superuser'), default=False)
avatar = models.ImageField(upload_to='avatars/', null=True, blank=True)
<script type="text/javascript">
var header = function(){
var country_id = {{ user.country_id }};
var country = $('#input-payment-country option[value='+ country_id +']').html();
var user = '{{ user.get_full_name }}';
var address = '{{ user.get_full_address }}';
$('#payment-existing select option[value='+4+']').text(user +', '+country +', '+ address);
};
$(document).ready(header);
Answer the question
In order to leave comments, you need to log in
# country_id = models.CharField(_('country'), max_length=30, blank=True)
country = models.ForeignKey('Country', blank=True, null=True)
class Country(Model):
name = models.CharField(_('country'), max_length=30)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question