I am working on a django web app. everything is working fine on my local machine and then pushed the code live on Heroku. but it doesn't work on the live server. it gives the following error when I try to post a .csv file:
DataError at /freshleadaction
value too long for type character varying(20)
I have PostgreSQL database integrated. I deleted the migrations and then migrated the whole schema again. I think the problem is either PostgreSQL or heroku? I have two models which are as follows:
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)
my views.py function:
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')
full error trace back:
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']
CodePudding user response:
First of all, there is no model with max_length=20
in the sources codes that you've provided. So you need to search for it in other models.
Pay attention:
- In Python you need to use CamelCase in class names, so make edits like
class cash_buyer_model(models.Model):
->
class CashBuyerModel(models.Model):
- Because this is a Model so you no need to use it in the class names:
class CashBuyer(models.Model):
- I see that you've mentioned that you've already found the needed field, you need to push updated code on Heroku
CodePudding user response:
I know you said you "deleted the migrations and then migrated the whole schema again," but clearly something must be left over from your original schema.
I would suggest:
- Delete the entire database in Heroku if you don't care about the data:
heroku pg:reset DATABASE
. - Push up your latest code to Heroku (including the new migrations in a new
git commit
):git push heroku HEAD:main
. - Migrate the database:
heroku run python manage.py migrate
.