0

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 ?

11
  • 2
    you have a bigger, much bigger problem. You need to normalize your models. Commented Nov 21, 2016 at 14:37
  • For CharField() use blank=True and not null=True. 2 Scoops of Django has a good section on When to use null and blank Commented Nov 21, 2016 at 14:39
  • @e4c5 As I said, I'm stuck with Django. I have to normalize, what is that exactly ? Commented Nov 21, 2016 at 14:41
  • What you are asking is more of a code review, hence this question should be asked on Code Review instead of stackoverflow. Commented Nov 21, 2016 at 14:41
  • @evans_murithi Ok thank you to your advice ! It's a good way to learn ;) Commented Nov 21, 2016 at 14:42

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

3 Comments

Not sure that it's best workaround, because in this way constants located under particular application folder and locked by each app, I think it's not correct. I think good way if constants cover all applications and stored in one place.
Putting the file at yourapp/countries was just a suggestion, I agree that there might be a better place for it. I disagree that this 'locks' the setting in an app. Apps can import values from each other. This answer gets the OP up and running, they can always change it later. Personally, I wouldn't put the list of countries in settings.py, it doesn't feel like a per-project setting to me.
I mean "locked" in design case, because you need to import from app_name. Regarding store countries under app level, I probably agree with you, and it's up to developer, but for example, number of threads or something that application wide, better to store in deeper level.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.