Reputation: 6128
I have a question about importing python file from Django models.
I'm just creating a models form
and I have in this one a list of country
. The user can make a choice with a list.
But in my models.py
file, it's very ugly and I would like to know if it's possible to write the country list in a python file and call this file in the argument choices = COUNTRY_CHOICE
This is my models.py :
#-*- coding: utf-8 -*-
from django.db import models
from django.forms import ModelForm
# Sexe choice
CHOIX_SEXE = (
('M', 'Mâle'),
('F','Femelle'),
)
CHOIX_PAYS = (
('AFG','Afghanistan'),
('AF','Afghanistan'),
('ZA','Afrique du Sud'),
('AL','Albanie'),
('DZ','Algérie'),
('DE','Allemagne'),
('AD','Andorre'),
('AO','Angola'),
('AI','Anguilla'),
('AQ','Antarctique'),
('AG','Antigua-et-Barbuda'),
('AN','Antilles néerlandaises'),
('SA','Arabie saoudite'),
('AR','Argentine'),
('AM','Arménie'),
('AW','Aruba'),
('AU','Australie'),
('AT','Autriche'),
('AZ','Azerbaïdjan'),
('BJ','Bénin'),
('BS','Bahamas'),
('BH','Bahreïn'),
('BD','Bangladesh'),
('BB','Barbade'),
('PW','Belau'),
('BE','Belgique'),
('BZ','Belize'),
('BM','Bermudes'),
('BT','Bhoutan'),
('BY','Biélorussie'),
('MM','Birmanie'),
('BO','Bolivie'),
('BA','Bosnie-Herzégovine'),
('BW','Botswana'),
('BR','Brésil'),
('BN','Brunei'),
('BG','Bulgarie'),
('BF','Burkina Faso'),
('CA','Canada'),
('CV','Cap-Vert'),
('CL','Chili'),
('CN','Chine'),
('CY','Chypre'),
('CO','Colombie'),
('KM','Comores'),
('CG','Congo'),
('KP','Corée du Nord'),
('KR','Corée du Sud'),
('CR','Costa Rica'),
('HR','Croatie'),
('CU','Cuba'),
('DK','Danemark'),
('DJ','Djibouti'),
('DM','Dominique'),
('EG','Égypte'),
('AE','Émirats arabes unis'),
('EC','Équateur'),
('ER','Érythrée'),
('ES','Espagne'),
('EE','Estonie'),
('US','États-Unis'),
('ET','Éthiopie'),
('FI','Finlande'),
('FR','France'),
('GE','Géorgie'),
('GA','Gabon'),
('GM','Gambie'),
('GH','Ghana'),
('GI','Gibraltar'),
('GR','Grèce'),
('GD','Grenade'),
('GL','Groenland'),
('GP','Guadeloupe'),
('GU','Guam'),
('GT','Guatemala'),
('GN','Guinée'),
('GQ','Guinée équatoriale'),
('GW','Guinée-Bissao'),
('GY','Guyana'),
('GF','Guyane française'),
('HT','Haïti'),
('HN','Honduras'),
('HK','Hong Kong'),
('HU','Hongrie'),
('BV','Ile Bouvet'),
('CX','Ile Christmas'),
('NF','Ile Norfolk'),
('KY','Iles Cayman'),
('CK','Iles Cook'),
('FO','Iles Féroé'),
('FK','Iles Falkland'),
('FJ','Iles Fidji'),
('GS','Iles Géorgie du Sud et Sandwich du Sud'),
('HM','Iles Heard et McDonald'),
('MH','Iles Marshall'),
('PN','Iles Pitcairn'),
('SB','Iles Salomon'),
('SJ','Iles Svalbard et Jan Mayen'),
('TC','Iles Turks-et-Caicos'),
('VI','Iles Vierges américaines'),
('VG','Iles Vierges britanniques'),
('CC','Iles des Cocos (Keeling),'),
('UM','Iles mineures éloignées des États-Unis'),
('IN','Inde'),
('ID','Indonésie'),
('IR','Iran'),
('IQ','Iraq'),
('IE','Irlande'),
('IS','Islande'),
('IL','Israël'),
('IT','Italie'),
('JM','Jamaïque'),
('JP','Japon'),
('JO','Jordanie'),
('KZ','Kazakhstan'),
('KE','Kenya'),
('KG','Kirghizistan'),
('KI','Kiribati'),
('KW','Koweït'),
('LA','Laos'),
('LS','Lesotho'),
('LV','Lettonie'),
('LB','Liban'),
('LR','Liberia'),
('LY','Libye'),
('LI','Liechtenstein'),
('LT','Lituanie'),
('LU','Luxembourg'),
('MO','Macao'),
('MG','Madagascar'),
('MY','Malaisie'),
('MW','Malawi'),
('MV','Maldives'),
('ML','Mali'),
('MT','Malte'),
('MP','Mariannes du Nord'),
('MA','Maroc'),
('MQ','Martinique'),
('MU','Maurice'),
('MR','Mauritanie'),
('YT','Mayotte'),
('MX','Mexique'),
('FM','Micronésie'),
('MD','Moldavie'),
('MC','Monaco'),
('MN','Mongolie'),
('MS','Montserrat'),
('MZ','Mozambique'),
('NP','Népal'),
('NA','Namibie'),
('NR','Nauru'),
('NI','Nicaragua'),
('NE','Niger'),
('NG','Nigeria'),
('NU','Nioué'),
('NO','Norvège'),
('NC','Nouvelle-Calédonie'),
('NZ','Nouvelle-Zélande'),
('OM','Oman'),
('UG','Ouganda'),
('UZ','Ouzbékistan'),
('PE','Pérou'),
('PK','Pakistan'),
('PA','Panama'),
('PG','Papouasie-Nouvelle-Guinée'),
('PY','Paraguay'),
('NL','Pays-Bas'),
('PH','Philippines'),
('PL','Pologne'),
('PF','Polynésie française'),
('PR','Porto Rico'),
('PT','Portugal'),
('QA','Qatar'),
('CF','République centrafricaine'),
('CD','République démocratique du Congo'),
('DO','République dominicaine'),
('CZ','République tchèque'),
('RE','Réunion'),
('RO','Roumanie'),
('GB','Royaume-Uni'),
('RU','Russie'),
('RW','Rwanda'),
('SN','Sénégal'),
('EH','Sahara occidental'),
('KN','Saint-Christophe-et-Niévès'),
('SM','Saint-Marin'),
('PM','Saint-Pierre-et-Miquelon'),
('VA','Saint-Siège' ),
('VC','Saint-Vincent-et-les-Grenadines'),
('SH','Sainte-Hélène'),
('LC','Sainte-Lucie'),
('SV','Salvador'),
('WS','Samoa'),
('AS','Samoa américaines'),
('ST','Sao Tomé-et-Principe'),
('SC','Seychelles'),
('SL','Sierra Leone'),
('SG','Singapour'),
('SI','Slovénie'),
('SK','Slovaquie'),
('SO','Somalie'),
('SD','Soudan'),
('LK','Sri Lanka'),
('SE','Suède'),
('CH','Suisse'),
('SR','Suriname'),
('SZ','Swaziland'),
('SY','Syrie'),
('TW','Taïwan'),
('TJ','Tadjikistan'),
('TZ','Tanzanie'),
('TD','Tchad'),
('TF','Terres australes françaises'),
('IO','Territoire britannique Océan Indien'),
('TH','Thaïlande'),
('TL','Timor Oriental'),
('TG','Togo'),
('TK','Tokélaou'),
('TO','Tonga'),
('TT','Trinité-et-Tobago'),
('TN','Tunisie'),
('TM','Turkménistan'),
('TR','Turquie'),
('TV','Tuvalu'),
('UA','Ukraine'),
('UY','Uruguay'),
('VU','Vanuatu'),
('VE','Venezuela'),
('VN','Viêt Nam'),
('WF','Wallis-et-Futuna'),
('YE','Yémen'),
('YU','Yougoslavie'),
('ZM','Zambie'),
('ZW','Zimbabwe'),
('MK','ex-République yougoslave de Macédoine'),
)
# Create my Form model
class BirthCertificate(models.Model) :
rowid = models.AutoField(primary_key=True)
nom = models.CharField('Nom', max_length=30, null=False) # Lastname
prenom = models.CharField('Prénom', max_length=30, null = False) # Firstname
sexe = models.CharField('Sexe', max_length=1, choices = CHOIX_SEXE) # Choice between 'M' or 'F'
birthday = models.DateField('Date de naissance', null=False)
birthhour = models.TimeField('heure de naissance', null=False)
birthcity = models.CharField('Ville de naissance', max_length = 30, null=False)
birthcountry = models.CharField('Pays de naissance', max_length=2, choices= CHOIX_PAYS)
nom_pere = models.CharField('Nom père', max_length=30, null=False)
prenom_pere = models.CharField('Prénom père', max_length=30, null=False)
birthday_pere = models.DateField('Date de naissance du père', null=False)
birthcity_pere = models.CharField('Ville de naissance du père', max_length=30, null=False)
birthcountry_pere = models.CharField('Pays de naissance du père', max_length=2, choices= CHOIX_PAYS)
job_pere = models.CharField('Profession du père', max_length=30, null=False)
adress_pere = models.CharField('Adresse du père', max_length=40, null=False)
ville_pere = models.CharField('Ville du père', max_length=30, null=False)
zip_pere = models.IntegerField('Code Postal du père', null=False)
pays_pere = models.CharField('Pays du père', max_length=2, choices= CHOIX_PAYS)
nom_mere = models.CharField('Nom mère', max_length=30, null=False)
prenom_mere = models.CharField('Prénom mère', max_length=30, null=False)
birthday_mere = models.DateField('Date de naissance de la mère', null=False)
birthcity_mere = models.CharField('Ville de naissance de la mère', max_length=30, null=False)
birthcountry_mere = models.CharField('Pays de naissance de la mère', max_length=2, choices= CHOIX_PAYS)
job_mere = models.CharField('Profession de la mère', max_length=30, null=False)
adress_mere = models.CharField('Adresse de la mère', max_length=40, null=False)
ville_mere = models.CharField('Ville de la mère', max_length=30, null=False)
zip_mere = models.IntegerField('Code Postal de la mère', null=False)
pays_mere = models.CharField('Pays de la mère', max_length=2, choices= CHOIX_PAYS)
nom_temoin1 = models.CharField('Nom témoin n°1', max_length=30, null=False)
prenom_temoin1 = models.CharField('Prénom témoin n°1', max_length=30, null=False)
birthday_temoin1 = models.DateField('Date de naissance du témoin n°1', null=False)
birthcity_temoin1 = models.CharField('Ville de naissance du témoin n°1', max_length=30, null=False)
nom_temoin2 = models.CharField('Nom témoin n°2', max_length=30, null=False)
prenom_temoin2 = models.CharField('Prénom témoin n°2', max_length=30, null=False)
birthday_temoin2 = models.DateField('Date de naissance du témoin n°2', null=False)
birthcity_temoin2 = models.CharField('Ville de naissance du témoin n°2', max_length=30, null=False)
def __str__(self):
return self.nom
return self.prenom
return self.sexe
I assume I need to create a python file, write the list of country and how I can call this list in my models.py ?
Thank you so much.
PS :
1) If you find some mistakes, because I'm beginning Django, you can tell me where and I will correct them ;)
2) If I want to separate the form in blocks, it's in the models.py or with html ?
Upvotes: 0
Views: 560
Reputation:
I agree with @Alasdair answer, his solution correct, but also I wanted to notice that system wide constants better store in main application folder ( near settings.py
) or in ( if it looks like settings variables ) settings, include it through settings.py
like here : Using Constants in Settings.py
Also, I want to suggest use this package for countries list : https://github.com/SmileyChris/django-countries
And here, this code always return first line, so you need remove other two:
def __str__(self):
return self.nom
return self.prenom
return self.sexe
Upvotes: 1
Reputation: 308799
Create a countries.py
in the same directory as your models.py
.
# yourapp/countries.py
CHOIX_PAYS = (
('AFG','Afghanistan'),
('AF','Afghanistan'),
...
)
Then in your models.py
, import CHOIX_PAYS
.
# yourapp/models.py
from .countries import CHOIX_PAYS
Upvotes: 2