Я учусь Django и следую этому онлайн-уроку по созданию форм. https://www.youtube.com/watch?v=6oOHlcHkX2U&list=PLEsfXFp6DpzTD1BD1aWNxS2Ep06vIkaeW&index=23. После рендеринга в браузере получено ограничение NOT NULL: product_product.price.
В моем models.py у меня есть
from django.db import models
# Create your models here.
class Product(models.Model):
title = models.CharField(max_length = 200, null = False, blank = False)
description = models.TextField(null = True, blank = True)
price = models.DecimalField(decimal_places=0, max_digits = 10)
active = models.BooleanField(default = True)
def __str__(self):
return self.title
В моем views.py под тем же приложением у меня есть
from django.shortcuts import render
from .forms import ProductForm
# Create your views here.
def productCreateView(request):
form = ProductForm(request.POST or None)
if form.is_valid:
print('form is valid now to saving')
print('\n\n')
form.save()
context = {'form' : form}
return render(request, '../templates/product_create.html', context=context)
Я пытался
python manage.py makemigrations
python manage.py migrate
когда я пытался изменить свою цену в моем models.py, например, указав
price = models.DecimalField(decimal_places=0, max_digits = 10, blank = False, null = False)
Каждый раз, когда говорится, что мигрировать нечего.
Ради обучения кто-нибудь может сказать мне, что мне не хватает?
Мои полные сообщения об ошибках выглядят как ниже
[09/Mar/2020 23:24:41] "GET /create HTTP/1.1" 301 0
form is valid now to saving
Internal Server Error: /create/
Traceback (most recent call last):
File "C:\Python\dimsumshop\env\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "C:\Python\dimsumshop\env\lib\site-packages\django\db\backends\sqlite3\base.py", line 396, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: NOT NULL constraint failed: product_product.price
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Python\dimsumshop\env\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Python\dimsumshop\env\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Python\dimsumshop\env\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python\dimsumshop\shop\product\views.py", line 11, in productCreateView
form.save()
File "C:\Python\dimsumshop\env\lib\site-packages\django\forms\models.py", line 459, in save
self.instance.save()
File "C:\Python\dimsumshop\env\lib\site-packages\django\db\models\base.py", line 746, in save
force_update=force_update, update_fields=update_fields)
File "C:\Python\dimsumshop\env\lib\site-packages\django\db\models\base.py", line 784, in save_base
force_update, using, update_fields,
File "C:\Python\dimsumshop\env\lib\site-packages\django\db\models\base.py", line 887, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
File "C:\Python\dimsumshop\env\lib\site-packages\django\db\models\base.py", line 926, in _do_insert
using=using, raw=raw,
File "C:\Python\dimsumshop\env\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Python\dimsumshop\env\lib\site-packages\django\db\models\query.py", line 1204, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "C:\Python\dimsumshop\env\lib\site-packages\django\db\models\sql\compiler.py", line 1391, in execute_sql
cursor.execute(sql, params)
File "C:\Python\dimsumshop\env\lib\site-packages\django\db\backends\utils.py", line 100, in execute
return super().execute(sql, params)
File "C:\Python\dimsumshop\env\lib\site-packages\django\db\backends\utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Python\dimsumshop\env\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Python\dimsumshop\env\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "C:\Python\dimsumshop\env\lib\site-packages\django\db\utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Python\dimsumshop\env\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "C:\Python\dimsumshop\env\lib\site-packages\django\db\backends\sqlite3\base.py", line 396, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: NOT NULL constraint failed: product_product.price
[09/Mar/2020 23:24:41] "GET /create/ HTTP/1.1" 500 145386