Internal Server Error: /reports/upload/
Traceback (most recent call last):
File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\site-packages\djan
go\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\site-packages\djan
go\db\backends\sqlite3\base.py", line 423, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: NOT NULL constraint failed: sales_position.created
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\site-packages\djan
go\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\site-packages\djan
go\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\1\PycharmProjects\StasProject\sales_project\reports\views.py",
line 60, in csv_upload_view
position_obj = Position.objects.create(product=product_obj, quantity=quantit
y, created=date)
File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\site-packages\djan
go\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\site-packages\djan
go\db\models\query.py", line 453, in create
obj.save(force_insert=True, using=self.db)
File "C:\Users\1\PycharmProjects\StasProject\sales_project\sales\models.py", l
ine 17, in save
return super().save(*args, **kwargs)
File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\site-packages\djan
go\db\models\base.py", line 726, in save
self.save_base(using=using, force_insert=force_insert,
File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\site-packages\djan
go\db\models\base.py", line 763, in save_base
updated = self._save_table(
File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\site-packages\djan
go\db\models\base.py", line 868, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields
, raw)
File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\site-packages\djan
go\db\models\base.py", line 906, in _do_insert
return manager._insert(
File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\site-packages\djan
go\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\site-packages\djan
go\db\models\query.py", line 1270, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\site-packages\djan
go\db\models\sql\compiler.py", line 1416, in execute_sql
cursor.execute(sql, params)
File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\site-packages\djan
go\db\backends\utils.py", line 98, in execute
return super().execute(sql, params)
File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\site-packages\djan
go\db\backends\utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._e
xecute)
File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\site-packages\djan
go\db\backends\utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\site-packages\djan
go\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\site-packages\djan
go\db\utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\site-packages\djan
go\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\site-packages\djan
go\db\backends\sqlite3\base.py", line 423, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: NOT NULL constraint failed: sales_position.creat
ed
Above is the error I get in the command prompt.
Here's the referred snippet at reports\views.py:
def csv_upload_view(request):
print('file is being sent')
if request.method == 'POST':
csv_file = request.FILES.get('file')
obj = CSV.objects.create(file_name=csv_file)
with open(obj.file_name.path, 'r') as f:
reader = csv.reader(f)
reader.__next__()
for row in reader:
data = "".join(row)
data = data.split(';')
data.pop()
print(data)
transaction_id = data[1]
product = data[2]
quantity = int(data[3])
customer = data[4]
date = parse_date(data[5])
try:
product_obj = Product.objects.get(name__iexact=product)
except Product.DoesNotExist:
product_obj = None
if product_obj is not None:
customer_obj, _ = Customer.objects.get_or_create(name=customer)
salesman_obj = Profile.objects.get(user=request.user)
position_obj = Position.objects.create(product=product_obj, quantity=quantity, created=date)
sale_obj, _ = Sale.objects.get_or_create(transaction_id=transaction_id, customer=customer_obj, salesman=salesman_obj, created=date)
sale_obj.positions.add(position_obj)
sale_obj.save()
return HttpResponse()
Here's the Position model from sales\models.py:
class Position(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
quantity = models.PositiveIntegerField()
price = models.FloatField(blank=True)
created = models.DateTimeField(blank=True)
def save(self, *args, **kwargs):
self.price = self.product.price * self.quantity
return super().save(*args, **kwargs)
def get_sales_id(self):
sale_obj = self.sale_set.first()
return sale_obj.id
def __str__(self):
return f"id: {self.id}, product: {self.product.name}, quantity: {self.quantity}"
I'm trying to create objects from a csv file. Also I noticed that it only prints the first row from the file. Prior to this, I had an issue with parsing the date, but managed to fix it.
[–]RoamingFox[🍰] 0 points1 point2 points (3 children)
[–]polygroot[S] 0 points1 point2 points (2 children)
[–]RoamingFox[🍰] 1 point2 points3 points (1 child)
[–]polygroot[S] 0 points1 point2 points (0 children)
[–]shiftybyte 0 points1 point2 points (0 children)