我需要一个更好的解决方案来实现我下面的目标。我有一个表格,这个表格需要为下列项目指定代码的顺序。。。你知道吗
class ContactCSVModel(CsvModel):
first_name = CharField()
last_name = CharField()
company = CharField()
mobile = CharField()
group = DjangoModelField(Group)
contact_owner = DjangoModelField(User)
例如…
class ContactCSVModel(CsvModel):
if form.col1.value == "first_name":
first_name = CharField()
elif form.col1.value == "last_name":
last_name = CharField()
或者
class ContactCSVModel(CsvModel):
[column.col1.value] = CharField()
[column.col2.value] = CharField()
但这不起作用,因为它将尝试赋值而不是将其设为第一个\u name=CharField()等
正如你所看到的,我最后对col2,col3等做了同样的操作,最后得到了一大堆if语句。你知道吗
有没有更好的方法,例如?
谢谢。你知道吗
表单.py
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')
这是我的原班人马:
def import_data(column, *args, **kw):
# make custom ContactCSVModel
class ContactCSVModel(CsvModel):
# IF column == x
first_name = CharField()
mobile = CharField()
last_name = CharField()
company = CharField()
group = DjangoModelField(Group)
contact_owner = DjangoModelField(User)
class Meta:
delimiter = ","
dbModel = Contact
update = {'keys': ["mobile", "group"]}
return ContactCSVModel.import_data(*args, **kw)
您能否在以后将字段添加到模型中,例如:
例如(使用Django 1.5):
相关问题 更多 >
编程相关推荐