/freshleadaction 值的 DataError 对于类型字符变化来说太长 (20)

DataError at /freshleadaction value too long for type character varying(20)

我正在开发一个 Django 网络应用程序。 在我的本地机器上一切正常,然后将代码推送到 Heroku 上。但它在实时服务器上不起作用。 当我尝试 post .csv 文件时出现以下错误:

DataError at /freshleadaction
value too long for type character varying(20)

我集成了 PostgreSQL 数据库。我删除了迁移,然后再次迁移了整个架构。我认为问题是 PostgreSQL 还是 heroku? 我有两个型号如下:

class fresh_leads_model(models.Model):
    fname = models.CharField(max_length=250)
    lname = models.CharField(max_length=250)
    street_number = models.CharField(max_length=250)
    street_name = models.CharField(max_length=250)
    state = models.CharField(max_length=250)
    zip_code = models.CharField(max_length=250)
    bedrooms = models.CharField(max_length=250)
    legal_description = models.CharField(max_length=250)
    sq_ft = models.CharField(max_length=250)
    address = models.CharField(max_length=250)
    orign_ln_amt = models.CharField(max_length=250)
    prop_value = models.CharField(max_length=250)
    equity = models.CharField(max_length=250)
    email = models.CharField(max_length=250)
    cell = models.CharField(max_length=250)
    submitted_date = models.DateField(auto_now_add=True)
    updated_date = models.DateField(auto_now_add=True)
    deleted_date = models.DateField(auto_now_add=True)


class cash_buyer_model(models.Model):
    fname = models.CharField(max_length=255)
    lname = models.CharField(max_length=255)
    email = models.CharField(max_length=255)
    city = models.CharField(max_length=255)
    state = models.CharField(max_length=255)
    submitted_date = models.DateField(auto_now_add=True)
    updated_date = models.DateField(auto_now_add=True)
    deleted_date = models.DateField(auto_now_add=True)

我的views.py函数:

def upload_fresh_leads(request):
    get_type = request.GET['type']
    lst = []
    if request.method == 'POST':
        leads = Fresh_leads_Form(request.POST, request.FILES)

        data = request.FILES.getlist('csv')
        # data = Fresh_leads_Form(request.FILES)
        # csv_file = request.GET['csv']
        # df = pd.read_csv(csv_file)
        # return HttpResponse(print(df)))

        # data = []

        # message = 2
        # return render(request, 'admin/src/tabs/fresh_leads.html', {'message': message})
        if get_type == '1':
            
            if leads.is_valid():
                # csv = data['csv']
                for d in data:
                    print('dataaaa :::::::::::', d)
                    # df = pd.read_csv(d, usecols=['Owner First Name'])
                    # return HttpResponse(df.to_string())

                    df = pd.read_csv(
                        d,
                        # print('dataaaa :::::::::::', d),
                        dtype='str',
                        
                        names=[
                            'Owner First Name',
                            'Owner Last Name',
                            'StreetNumber',
                            'StreetName',
                            'State',
                            'Zip Code',
                            'Bdrms',
                            'Legal Description',
                            'Sq Ftg',
                            'Address',
                            'Orig Ln Amt',
                            'Prop Value',
                            'Equity',
                            'Email',
                            'Cell'
                        ])
                        
                    # print('data frame', df)
                    df = df.fillna(0)
                    df.columns = [
                        'Owner_FirstName',
                        'Owner_LastName',
                        'StreetNumber',
                        'StreetName',
                        'State',
                        'ZipCode',
                        'Bdrms',
                        'Legal_Description',
                        'Sq_Ftg',
                        'Address',
                        'Orig_Ln_Amt',
                        'Prop_Value',
                        'Equity',
                        'Email',
                        'Cell']

                    json_record = df.reset_index().to_json(orient='records')

                    lst = json.loads(json_record)

                    bulk_data = df.to_dict('records')

                    model_instances = [fresh_leads_model(
                        fname=i['Owner_FirstName'],
                        lname=i['Owner_LastName'],
                        street_number=i['StreetNumber'],
                        street_name=i['StreetName'],
                        state=i['State'],
                        zip_code=i['ZipCode'],
                        bedrooms=i['Bdrms'],
                        legal_description=i['Legal_Description'],
                        sq_ft=i['Sq_Ftg'],
                        address=i['Address'],
                        orign_ln_amt=i['Orig_Ln_Amt'],
                        prop_value=i['Prop_Value'],
                        equity=i['Equity'],
                        email=i['Email'],
                        cell=i['Cell']
                    )for i in bulk_data]
                    fresh_leads_model.objects.bulk_create(model_instances)

                # message = 2
                # return render(request, 'admin/src/tabs/fresh_leads.html', {'data': lst, 'message': message})
                return redirect('main:FreshLeadView')
        # message = 1
        # return render(request, 'admin/src/tabs/fresh_leads.html', {'message': message})

        # message = 3
        # return render(request, 'admin/src/tabs/fresh_leads.html', {'message': message})

    return render(request, 'frontend-layout/500.html')

完整的错误回溯:

DataError at /freshleadaction
value too long for type character varying(20)
Request Method: POST
Request URL:    http://www.realtorbit.com/freshleadaction?type=1
Django Version: 3.1.1
Exception Type: DataError
Exception Value:    
value too long for type character varying(20)
Exception Location: /app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py, line 84, in _execute
Python Executable:  /app/.heroku/python/bin/python
Python Version: 3.6.1
Python Path:    
['/app/.heroku/python/bin',
 '/app',
 '/app/.heroku/python/lib/python36.zip',
 '/app/.heroku/python/lib/python3.6',
 '/app/.heroku/python/lib/python3.6/lib-dynload',
 '/app/.heroku/python/lib/python3.6/site-packages']

首先,您提供的源代码中没有带有max_length=20的模型。所以你需要在其他模型中搜索它。 关注:

  1. 在 Python 中,您需要在 class 名称中使用 CamelCase,因此请像
  2. 这样进行编辑
class cash_buyer_model(models.Model):

->

class CashBuyerModel(models.Model):
  1. 因为这是一个模型,所以您不需要在 class 名称中使用它:
class CashBuyer(models.Model):
  1. 我看到你提到你已经找到了需要的字段,你需要push updated code on Heroku

我知道你说你“删除了迁移,然后再次迁移了整个模式”,但显然你的原始模式中必须遗留一些东西。

我建议:

  1. 如果您不关心数据,请删除 Heroku 中的整个数据库:heroku pg:reset DATABASE
  2. 将您的最新代码推送到 Heroku(包括新 git commit 中的新迁移):git push heroku HEAD:main.
  3. 迁移数据库:heroku run python manage.py migrate.

大家好,感谢你们的努力。实际上,在将我的问题发布到这里之前,我应用了所有这些解决方案,但突然间我有了一个想法,我从我的 fresh_leads_model 和 [=18= 中删除了所有 Charfields ]cash_buyer_model 并将它们的参数留空(我的意思是没有 max_length=255)。虽然 CharField 在我的本地机器上工作,但在 在线服务器 上没有工作。所以我决定将它从 CharField 更改为 TextField 没有 max_length 限制所以 Heroku 支持它并且现在一切正常。