我使用Django时遇到了第一个真正的问题,因为我是新手,我无法弄明白。
我正在使用Django-adaptors(虽然我对其他建议持开放态度,但它会映射到模型并且运行良好)。无论如何,我想导入数据,但要确保订单是由用户指定的。
这就是我到目前为止......
Forms.py
此表单会向用户显示,然后他们会选择哪一列代表其CSV上传中的字段。
COL_CHOICES = [
('NONE', 'No Import'),
('first_name', 'First Name'),
('last_name', 'Last Name'),
('company', 'Company'),
('mobile', 'Mobile Number'),
('email', 'Email Address'),
]
class ConfiguratorForm(forms.Form):
col1 = forms.TypedChoiceField(choices=COL_CHOICES, initial='first_name')
col2 = forms.TypedChoiceField(choices=COL_CHOICES, initial='first_name')
col3 = forms.TypedChoiceField(choices=COL_CHOICES, initial='first_name')
col4 = forms.TypedChoiceField(choices=COL_CHOICES, initial='first_name')
col5 = forms.TypedChoiceField(choices=COL_CHOICES, initial='first_name')
返回我的view.py POST到以下内容......
if request.method == 'POST':
form = ConfiguratorForm(data=request.POST)
# Try and import CSV
ContactCSVModel.import_data(data=upload.filepath, extra_fields=[
{'value': upload.group_id, 'position': 5},
{'value': upload.uploaded_by.id, 'position': 6}])
现在是问题。
class ContactCSVModel(CsvModel):
first_name = CharField()
last_name = CharField()
company = CharField()
mobile = CharField()
group = DjangoModelField(Group)
contact_owner = DjangoModelField(User)
class Meta:
delimiter = ","
dbModel = Contact
update = {'keys': ["mobile", "group"]}
如上所示,它目前仅按照上面列出的顺序匹配first_name,last_name等,而不是用户指定的顺序。
我该怎么做?