我在django_countries
国家列表中使用模块,问题是有几个国家有特殊字符'Åland Islands'
和'Saint Barthélemy'
.
我正在调用此方法来获取国家/地区名称:
country_label = fields.Country(form.cleaned_data.get('country')[0:2]).name
Run Code Online (Sandbox Code Playgroud)
我知道country_label是django utils的懒惰翻译代理对象,但它没有给出正确的名称,而是给出了它'Ã…land Islands'
.对此有何建议?
我django_countries
用来显示国家列表.现在,我有一个要求,我需要根据国家显示货币.挪威 - 挪威克朗,欧洲和非洲(除英国外) - 欧元,英国 - 英镑,美国和亚洲 - 美元.
这可以通过django_countries项目实现吗?或者我可以使用python或django中的其他软件包吗?
任何其他解决方案也受到欢迎.
---------------------------更新-------------主要重点在于获得很多解决方案:
Norway - NOK, Europe & Afrika (besides UK) - EUR, UK - GBP, AMERICAS & ASIA - USDs.
---------------------------- SOLUTION --------------------- -----------
我的解决方案很简单,当我意识到我无法获得任何ISO格式或包来获得我想要的东西时,我想编写自己的脚本.它只是一个基于条件的逻辑:
from incf.countryutils import transformations
def getCurrencyCode(self, countryCode):
continent = transformations.cca_to_ctn(countryCode)
# print continent
if str(countryCode) == 'NO':
return 'NOK'
if str(countryCode) == 'GB':
return 'GBP'
if (continent == 'Europe') or (continent == 'Africa'):
return 'EUR'
return 'USD'
Run Code Online (Sandbox Code Playgroud)
不知道这是否有效,想听听一些建议.
感谢大家!
我使用Django的城市光(打火机版Django的城市)和Django 1.8.x. 它定义了Country,Region/State和City的抽象模型,以便我们可以扩展和添加自定义字段.例如,我们可以通过编写一个post_import信号处理程序说明添加时区城市在这里.
同样,我需要将字段添加capital
到每个国家/地区.我对GeoDjango并不熟悉,我知道django-cities
app的Country有资本领域.
我知道有几个这样的问题(例如这个),但没有一个可以帮助我解决我的问题。
我想在我的模型中有一个城市和一个国家字段,城市的选择取决于国家;但我不想将 City 和 Country 定义为模型类。这是我的代码:
from django.contrib.auth.models import User
from django.db import models
from django.forms import ChoiceField
from django_countries.fields import CountryField
class UserProfile(models.Model):
user = models.OneToOneField(User, related_name="UserProfile")
name = models.CharField(max_length=30, null=False, blank=False)
picture = models.ImageField(upload_to='userProfiles/', null=False, blank=False)
date_of_birth = models.DateTimeField(null=False, blank=False)
country = CountryField()
# city = ??
national_code = models.IntegerField(max_length=10, null=False, blank=False)
email = models.EmailField()
def __str__(self):
return '{}'.format(self.user.username)
def __unicode__(self):
return self.user.username
Run Code Online (Sandbox Code Playgroud)
就像字段“国家”一样country = CountryField()
,我想知道是否有一种方法可以在不定义class Country(models.Model)
或的情况下完成任务class City(models.Model)
我正在尝试在将与 django-allauth 一起使用的注册表中添加 django-countries。按照说明https://github.com/SmileyChris/django-countries我创建了一个模型
class UserProfile(models.Model):
# Other things
country = CountryField()
Run Code Online (Sandbox Code Playgroud)
还有一个表格
从 django_countries.data 导入国家
class SignupForm(forms.Form):
# Other stuff
country = forms.ChoiceField(choices=COUNTRIES, required=True)
def signup(self, request, user):
# Other Stuff
user.userprofile.country = self.cleaned_data['country']
Run Code Online (Sandbox Code Playgroud)
但是当我访问 /accounts/signup/ 页面时,我收到了表格,但对于我选择的国家/地区
<p><label for="id_country">Country:</label> <select id="id_country" name="country">
<option value="G">Q</option>
<option value="I">D</option>
<option value="K">Y</option>
...
Run Code Online (Sandbox Code Playgroud)
代替国家代码和国家名称
我正在使用Django 2.x
。
我有一个模型user_setting
,该模型具有Country
在address
模块下创建的模型的外键。
现在,我djanog-countries-plus
用于添加国家/地区数据,因此更改了user_setting
模块,使其现在指向的国家/地区模型django-countries-plus
。
from countries_plus.models import Country
from django.contrib.auth.models import User
class UserSetting(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
user = models.OneToOneField(User, on_delete=models.CASCADE)
country = models.ForeignKey(Country, on_delete=models.PROTECT, blank=True, null=True, default=None)
modified = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True)
Run Code Online (Sandbox Code Playgroud)
最初产生的迁移是
# 000_initial
class Migration(migrations.Migration):
initial = True
dependencies = [
('address', '0001_initial'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='UserSetting',
fields=[
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), …
Run Code Online (Sandbox Code Playgroud) 我今天安装了django国家.它的工作非常整洁.
不幸的是,文档有点薄,它说:
国家/地区由两部分元组组成的元组,每个元组由一个国家/地区代码和相应的标题清晰(可翻译)的国家/地区名称组成.
我是否只是沿着django国际化的常规路径走下去,它会自动选择并允许我在我的PO文件中定义国家/地区?或者还需要额外的魔法吗?
我实现它的方式是这样的:
from django_countries.countries import COUNTRIES
from django_countries.fields import CountryField
country = CountryField(_(u'Country'), choices=COUNTRIES, blank=True)
Run Code Online (Sandbox Code Playgroud)
COUNTRIES
直接来自扩展名,因此我没有权限放置_(u)
每个国家/地区名称.(from django.utils.translation import ugettext_lazy as _
)
我已经使用pip install django_countries安装了Django国家,但仍然出现ImportError错误:无法导入名称CountryField:
(simpli)Allens-MacBook-Air:~/Sites/augmify/simpli [master]$ python manage.py syncdb
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/allenchun/.virtualenvs/simpli/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/Users/allenchun/.virtualenvs/simpli/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/allenchun/.virtualenvs/simpli/lib/python2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Users/allenchun/.virtualenvs/simpli/lib/python2.7/site-packages/django/core/management/base.py", line 284, in execute
self.validate()
File "/Users/allenchun/.virtualenvs/simpli/lib/python2.7/site-packages/django/core/management/base.py", line 310, in validate
num_errors = get_validation_errors(s, app)
File "/Users/allenchun/.virtualenvs/simpli/lib/python2.7/site-packages/django/core/management/validation.py", line 34, in get_validation_errors
for (app_name, error) in get_app_errors().items():
File "/Users/allenchun/.virtualenvs/simpli/lib/python2.7/site-packages/django/db/models/loading.py", line 196, in get_app_errors
self._populate()
File …
Run Code Online (Sandbox Code Playgroud)