When typing the code, python manage.py shell--->aa.save()
, this error appears:
django.db.utils.IntegrityError: (1048, 'Unknown error 1048')
How can I solve it?
Following is my code,
class Article(models.Model):
title=models.CharField(max_length=50)
author=models.CharField(max_length=50)
created_date=models.DateField(auto_now_add=True)
modify_date=models.DateField(auto_now=True)
content=models.TextField()
is_show=models.BooleanField()
class Meta:
db_table="article"
def __str__(self):
return self.title
>>> from guestbook.models import Article
>>> Article.objects.all()
<QuerySet []>
>>> aa = Article()
>>> aa
<Article: >
>>> aa.title
''
>>> aa.title = "hello"
>>> aa.title
'hello'
>>> aa.save()
Error
File "C:\\path\\to\\directory\\containing\\other\\environments\\site\\lib\\site-packages\\pymysql\\connections.py", line 683, in _read_packet packet.check_error()
File "C:\\path\\to\\directory\\containing\\other\\environments\\site\\lib\\site-packages\\pymysql\\protocol.py", line 220, in check_error err.raise_mysql_exception(self._data)
File "C:\\path\\to\\directory\\containing\\other\\environments\\site\\lib\\site-packages\\pymysql\\err.py", line 109, in raise_mysql_exception raise errorclass(errno, errval) django.db.utils.IntegrityError: (1048, 'Unknown error 1048')
This might be because you're trying to save a model instance with required fields missing. You can add null=True
or a default values in your model attributes. For example:
class Article(models.Model):
title=models.CharField(max_length=50)
author=models.CharField(max_length=50, null=True)
created_date=models.DateField(auto_now_add=True)
modify_date=models.DateField(auto_now=True)
content=models.TextField(null=True)
is_show=models.BooleanField(default=False)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.