S
S
Sergey Ganzhela2016-11-18 14:19:32
Django
Sergey Ganzhela, 2016-11-18 14:19:32

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>

that's a piece of code! value id value
class CreationFormUser(forms.ModelForm):
    class Meta:
        model = User
        fields = ('email', 'last_name', 'first_name', 'address', 'post_code', 'password', 'country_id', 'phone', 'city')

here is the form
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)

here is Yuzver's model
<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);

here's a crutch :)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sim3x, 2016-11-18
@Sergiy_Hanzhela

# 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)

And from the Country model you will make a jang form, validate, etc.

I
iegor, 2016-11-18
@iegor

https://docs.djangoproject.com/en/1.10/ref/models/... do you mean this?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question