我设置 null = 真和空白 = 真,但仍然得到"django.db.utils.IntegrityError: (1048, "列 'massenger_name' 不能为空")"



我试图运行我的页面,但我得到了这个错误"django.db.utils.IntegrityError:(1048,"列'massenger_name'不能为null");

我再次进行迁移和imgrate,但我仍然有同样的问题

我的数据库是mysql

这是我的型号.py

from django.db import models
# Create your models here.
class Name(models.Model):
massenger_name = models.CharField(max_length=255,null=True,blank=True)
action_time = models.DateTimeField(auto_now_add=True)
def __str__(self):
return str(self.massenger_name)

这是我的观点.py

from django.shortcuts import render
from .models import Name
from django.shortcuts import redirect
# Create your views here.
def Home(request):
name_input = request.POST.get('user_name')
name_in_model = Name(massenger_name=name_input)
name_in_model.save()
return render(request , 'index.html')

这是我的index.html

<!DOCTYPE html>
<html lang="en" dir="rtl">
<head>
<meta charset="utf-8">
<title></title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous">
{% load static %}
<link rel="stylesheet" href="{% static 'CSS/style.css' %}">
</head>
<body>
<div id="form">
<form class="row" method="POST">
{% csrf_token %}
<div class="">
<input type="textarea" class="form-control" placeholder="أكتب أسمك (مثلا/أخوكم عبدالله العتيبي)" id="text_name" name="user_name" required>
</div>
<div class="col-auto">
<button type="submit" class="btn btn-primary mb-3" id="button">حمل الصورة</button>
</div>
</form>
</div>
</body>
</html>

这是我的设置.py

"""
Django settings for Eid_G project.
Generated by 'django-admin startproject' using Django 3.2.5.
For more information on this file, see
https://docs.djangoproject.com/en/3.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
"""
from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
TEMPLATES_DIR = os.path.join(BASE_DIR,"templates")
STATIC_DIR = os.path.join(BASE_DIR,"static")

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = ''
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'Eid_Post',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'Eid_G.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATES_DIR,],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'Eid_G.wsgi.application'

# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django_p',
'HOST': 'localhost',
'PORT': '3306',
'USER': 'root',
'PASSWORD': '',
}
}

# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]

# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [STATIC_DIR,]
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

非常感谢

之所以会发生这种情况,是因为数据库中已经有模型的一些数据,因此需要首先删除这些数据。

您可以简单地从django-shell中删除表中的所有对象:

转到命令提示符

1. python manage.py shell
2. from main.models import Name
3. Name.objects.all().delete()
(main being the app name here)

这将删除Tutorial表中的所有对象,然后进行迁移和迁移,应该可以正常工作。

或者,如果它仍然不起作用,你可以尝试以下方法:

1. delete the migrations file (folder) of the app and makemigrations and migrate again.
2. change the database of your project (but you can loss all of the site data by this.)

由于您已经删除了迁移,问题一定是Django已经忘记了表上的约束。您应该寻找一种直接从数据库中删除NOT NULL约束的方法。也许试试这个

相关内容

最新更新