I am new to Django (i am using Django 1.4 & python 2.7) and cannot understand how to accomplish the following issue.
I have done a lot of searching on SO & Google, and so far become very confused on how to do this problem.
I have two html select lists on a form - Industry & Sector. When a user selects Engineering from the Industry html select list, the Sector html select list should be dynamically filled with only Engineering options. The same with other selections made on the Industry html select list. The Sector html select list should be filled dynamically without a page refresh - so I am assuming that JQuery / AJAX will have to be used.
I am not so sure I have correctly set up the UserProfile model below. The Industry & Sector html select lists appear on the form and work, but are not dependent on each other - they are separate models.PositiveIntegerField fields. Perhaps the Industry & Sector values should be foreign keys on the UserProfile model below. I need some advice on this.
Here is my user models.py file:
class UserProfile(models.Model):
SELECT_INDUSTRY = 0
ACCOUNTING = 1
ADMINISTRATION_OFFICE_SUPPORT = 2
BANKING_FINANCIAL_SERVICES = 3
CALL_CENTRE_CUSTOMER_SERVICE = 4
COMMUNITY_SERVICES_DEVELOPMENT = 5
CONSTRUCTION = 6
CONSULTING_STRATEGY = 7
DESIGN_ARCHITECTURE = 8
EDUCATION_TRAINING = 9
ENGINEERING = 10
EXECUTIVE_GENERAL_MANAGEMENT = 11
FARMING_ANIMALS_CONSERVATION = 12
GOVERNMENT_DEFENCE = 13
GRADUATE_ENTRY_LEVEL = 14
HEALTHCARE_MEDICAL = 15
HOSPITALITY_TRAVEL_TOURISM = 16
HUMAN_RESOURCES_RECRUITMENT = 17
INSURANCE_SUPERANNUATION = 18
INFORMATION_TECHNOLOGY_TELECOMMUNICATIONS = 19
LEGAL = 20
MANUFACTURING = 21
MARKETING_COMMUNICATIONS = 22
MEDIA_ADVERTISING_ARTS_ENTERTAINMENT = 23
MINING_RESOURCES_ENERGY = 24
REAL_ESTATE_PROPERTY = 25
RETAIL_CONSUMER_PRODUCTS = 26
SALES = 27
SCIENCE_TECHNOLOGY = 28
SELF_EMPLOYMENT = 29
SPORT_RECREATION = 30
TRADES_SERVICES = 31
TRANSPORT_LOGISTICS = 32
USER_PROFILE_CURRENT_INDUSTRY_TYPES = (
(SELECT_INDUSTRY, _('Select Current Industry')),
(ACCOUNTING, _('Accounting')),
(ADMINISTRATION_OFFICE_SUPPORT, _('Administration & Office Support')),
(BANKING_FINANCIAL_SERVICES, _('Banking & Financial Services')),
(CALL_CENTRE_CUSTOMER_SERVICE, _('Call Centre & Customer Service')),
(COMMUNITY_SERVICES_DEVELOPMENT, _('Community Services & Development')),
(CONSTRUCTION, _('Construction')),
(CONSULTING_STRATEGY, _('Consulting & Strategy')),
(DESIGN_ARCHITECTURE, _('Design & Architecture')),
(EDUCATION_TRAINING, _('Education & Training')),
(ENGINEERING, _('Engineering')),
(EXECUTIVE_GENERAL_MANAGEMENT, _('Executive & General Management')),
(FARMING_ANIMALS_CONSERVATION, _('Farming, Animals & Conservation')),
(GOVERNMENT_DEFENCE, _('Government & Defence')),
(GRADUATE_ENTRY_LEVEL, _('Graduate / Entry Level')),
(HEALTHCARE_MEDICAL, _('Healthcare & Medical')),
(HOSPITALITY_TRAVEL_TOURISM, _('Hospitality, Travel & Tourism')),
(HUMAN_RESOURCES_RECRUITMENT, _('Human Resources & Recruitment')),
(INSURANCE_SUPERANNUATION, _('Insurance & Superannuation')),
(INFORMATION_TECHNOLOGY_TELECOMMUNICATIONS, _('Information Technology & Telecommunications')),
(LEGAL, _('Legal')),
(MANUFACTURING, _('Manufacturing')),
(MARKETING_COMMUNICATIONS, _('Marketing & Communications')),
(MEDIA_ADVERTISING_ARTS_ENTERTAINMENT, _('Media, Advertising, Arts & Entertainment')),
(MINING_RESOURCES_ENERGY, _('Mining, Resources & Energy')),
(REAL_ESTATE_PROPERTY, _('Real Estate & Property')),
(RETAIL_CONSUMER_PRODUCTS, _('Retail & Consumer Products')),
(SALES, _('Sales')),
(SCIENCE_TECHNOLOGY, _('Science & Technology')),
(SELF_EMPLOYMENT, _('Self Employment')),
(SPORT_RECREATION, _('Sport & Recreation')),
(TRADES_SERVICES, _('Trades & Services')),
(TRANSPORT_LOGISTICS, _('Transport & Logistics'))
)
SELECT_SECTOR_TYPE = 0
_ALL_ACCOUNTING_JOBS = 1
....(culled for brevity)
_ALL_ENGINEERING_JOBS = 124
AEROSPACE_ENGINEERING = 125
AUTOMOTIVE_ENGINEERING = 126
BUILDING_SERVICES_ENGINEERING = 127
CHEMICAL_ENGINEERING = 128
CIVIL_STRUCTURAL_ENGINEERING = 129
ELECTRICAL_ELECTRONIC_ENGINEERING = 130
ENGINEERING_DRAFTING = 131
ENVIRONMENTAL_ENGINEERING = 132
FIELD_ENGINEERING = 133
INDUSTRIAL_ENGINEERING = 134
MAINTENANCE = 135
MANAGEMENT = 136
MATERIALS_HANDLING_ENGINEERING = 137
MECHANICAL_ENGINEERING = 138
PROCESS_ENGINEERING = 139
PROJECT_ENGINEERING = 140
PROJECT_MANAGEMENT = 141
SUPERVISORS = 142
SYSTEMS_ENGINEERING = 143
WATER_WASTE_ENGINEERING = 144
OTHER_ENGINEERING_JOBS = 145
_ALL_EXECUTIVE_GENERAL_MANAGEMENT_JOBS = 146
....(culled for brevity)
OTHER_TRANSPORT_LOGISTICS_JOBS = 462
USER_PROFILE_CURRENT_SECTOR_TYPES = (
(SELECT_SECTOR_TYPE, _('Select Current Sector')),
.......(culled for brevity)
(OTHER_TRANSPORT_LOGISTICS_JOBS, _('Other Transport & Logistics Jobs'))
)
user = models.OneToOneField(User)
....(culled for brevity)
current_industry_type = models.PositiveIntegerField(choices=USER_PROFILE_CURRENT_INDUSTRY_TYPES, default=SELECT_INDUSTRY, validators=[MinValueValidator(1)])
current_sector_type = models.PositiveIntegerField(choices=USER_PROFILE_CURRENT_SECTOR_TYPES, default=SELECT_SECTOR_TYPE, validators=[MinValueValidator(1)])
....(culled for brevity)
.
I have seen django-smart-selects, but I am not sure this is a dynamic solution and I am not sure if I have to add in separate models for the Industry & Sector & then add the foreign keys for the Industry & Sector to the UserProfile model above.
I am hoping I can somehow easily get the Industry & Sector html select lists dependent on each other with AJAX or JQuery.
Any advice and help would be appreciated.