csv没有映射到django模型

时间:2014-10-31 15:42:01

标签: python django csv django-adaptors

我有一个应该运行但未运行的代码。我已经做了大量的研究而且令人沮丧,但似乎我只需要一双新鲜的眼睛 我使用过csv importer并略微调整了一下 代码应该上传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中我有:     来自credit.models导入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>

admin.py

class AnalysisAdmin(admin.ModelAdmin):
    class Media:        
        change_form_template = 'application/upload_csv.html'
        add_form_template = 'application/upload_csv.html'    

我做错了什么或做得不对?这意味着很简单,但却让人感到沮丧。任何帮助都可以。 P.S:我是django的新手 谢谢!

1 个答案:

答案 0 :(得分:0)

models.py首先定义Analysis然后定义CSV,如下所示:

from django.db import models
from adaptor.model import CsvModel
from application.models import Analysis

class Analysis(models.Model)
    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()

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 = ","