我有一个应该运行但没有运行的代码。我已经进行了大量的研究,这越来越令人沮丧,但似乎我只需要一双新的眼睛。
我使用了csv 导入器并对其进行了一些调整。
代码应该上传 csv,并将列读入 django 字段!中提琴!不难。
在models.py我有:
from adaptor.model import CsvModel
from application.models import Analysis
class CSV(CsvModel):
csv_file = models.FileField(upload_to=upload_to)
political_environment = DecimalField()
GDP_per_capita = DecimalField()
economic_diversification= DecimalField()
inflation= DecimalField()
monetary_credibility= DecimalField()
access_to_finance= DecimalField()
#This is my major issues as An error keeps popping up saying
#"Analysis model isn't defined" for dbModel despite defining it at the start
class Meta:
dbModel = Analysis
delimiter = ","
class Analysis
class Meta:
ordering = ['country']
verbose_name = _('Analysis')
political_environment = DecimalField()
GDP_per_capita = DecimalField()
economic_diversification= DecimalField()
inflation= DecimalField()
monetary_credibility= DecimalField()
access_to_finance= DecimalField()
在 forms.py 我有: from credit.models import CSV
class CSVForm(forms.ModelForm):
class Meta:
model = CSV
key_to_field_map = getattr(settings, 'CSVIMPORTER_KEY_TO_FIELD_MAP', lambda k: k.replace(' ','_').lower())
意见
from credit.models import CSV
from credit.forms import CSVForm,
def new(request):
if request.method == 'POST':
form = CSVForm(request.POST, request.FILES)
if form.is_valid():
instance = form.save()
request.user.message_set.create(message='Uploaded CSV. Please associate fields below.')
return HttpResponseRedirect(reverse('associate-csv',args=[instance.id]))
else:
form = CSVForm()
return render_to_response('new.html',
{'form':form}, context_instance=RequestContext(request))
html
{% block content %}
<div id="content-main">
<h2>Upload CSV</h2>
<form action="." method="post" accept-charset="utf-8" enctype="multipart/form-data">
<table>
{{form.as_table}}
</table>
<input type="submit" class="submit" />
</form>
</div>
<div class="accordion-body collapse{% if form.errors %} in{% endif %}" id="Data">
<div class="accordion-inner row-fluid" label for="id_Data">
<div class="span5">
{% control form.political_environment %}
{% control form.GDP_per_capita %}
{% control form.economic_diversification %}
{% control form.inflation %}
{% control form.monetary_credibility %}
{% control form.access_to_finance %}
</div>
</div>
</div>
管理员.py
class AnalysisAdmin(admin.ModelAdmin):
class Media:
change_form_template = 'application/upload_csv.html'
add_form_template = 'application/upload_csv.html'
我做错了什么或做错了什么?这本来很简单,但它越来越令人沮丧。任何帮助都可以。PS:我是django的新手谢谢!