Home > Net >  i set null = true and blank = true but still get "django.db.utils.IntegrityError: (1048, "
i set null = true and blank = true but still get "django.db.utils.IntegrityError: (1048, "

Time:07-06

im trying to run my page but i get this error "django.db.utils.IntegrityError: (1048, "Column 'massenger_name' cannot be null")"

and i makemigrations and imgrate again but still i have same problem

and my database is mysql

this my models.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)

and this my views.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')

and this my index.html

<!DOCTYPE html>
<html lang="en" dir="rtl">
  <head>
    <meta charset="utf-8">
    <title></title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/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  method="POST">
        {% csrf_token %}
          <div >
            <input type="textarea"  placeholder="أكتب أسمك (مثلا/أخوكم عبدالله العتيبي)" id="text_name" name="user_name" required>
          </div>
          <div >
            <button type="submit"  id="button">حمل الصورة</button>
          </div>
      </form>
    </div>

  </body>
</html>

and this my setting.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'

and thank u so much

CodePudding user response:

Since you've deleted your migrations, the problem must be that Django has lost track of what constraints are on your tables. You should look for a way to delete that NOT NULL constraint directly from the database. Maybe try this

CodePudding user response:

This is happening because you already have some data of the model in the database so you need to delete the data first.

You can simply delete all the objects in the table from django shell:

go to command prompt

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

This will delete all the objects in the Tutorial table and then makemigrations and migrate and it should work just fine.

or if it still don't work you can try these approaches:

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.)
  • Related