im new to django and am trying to figure out how to add a specific field to a django model. I want to upload a csv document and save its headers. As you can see, i want to use the headers i send with the document, or if they arent send, the ones from the first line of the document.
from django.db import models
class CSV_API_DB(models.Model):
headers = models.CharField(max_length=250, blank=True)
delimiter = models.CharField(max_length=1, default=';')
filefield = models.FileField(upload_to='uploads/', max_length=100, default='empty')
actual_headers = ''
def __str__(self):
actual_headers = ''
if not self.headers:
file_path = str(self.filefield)
file = open(file_path)
for line in file:
string_line = str(line[:-1])
actual_headers = string_line.split(self.delimiter)
break
else:
actual_headers = self.headers.split(self.delimiter)
return str(actual_headers)
true_headers = models.CharField(max_length=250, default = str(actual_headers), editable=False)
The issue seems to be, that true_headers does not get overridden from the '__ str __' function, since the values in the database for true_headers are always just empty strings.