i create a model in django and register it in admin.py when i go to admin panel it shows the model but when i want to create object it doesn’t show the charfields and i can just create the oject without any details
this is my codes below
view.py
from django.shortcuts import render from django.http import HttpResponse from .models import Feature # Create your views here. def index(request): features = Feature.objects.all() return render(request, 'index.html', {'features' : features})
model.py
from django.db import models # Create your models here. class Feature(models.Model): name: models.CharField(max_length=100) details: models.CharField(max_length=200)
stting.py
""" Django settings for myproject project. Generated by 'django-admin startproject' using Django 3.2.12. For more information on this file, see For the full list of settings and their values, see """ from pathlib import Path import os # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent # Quick-start development settings - unsuitable for production # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = 'django-insecure-dubzu2qq@tk9lk%d05a*@j1rd1hkr$v72eiga+*u7%v2d)19_5' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True ALLOWED_HOSTS = [] # Application definition INSTALLED_APPS = [ 'livesync', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'myapp' ] MIDDLEWARE = [ 'livesync.core.middleware.DjangoLiveSyncMiddleware', '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 = 'myproject.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [BASE_DIR, 'templates'], '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 = 'myproject.wsgi.application' # Database DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': BASE_DIR / 'db.sqlite3', } } # Password validation 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 LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' USE_I18N = True USE_L10N = True USE_TZ = True # Static files (CSS, JavaScript, Images) STATIC_URL = '/static/' STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),) # Default primary key field type DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
admin.py
from django.contrib import admin from .models import Feature # Register your models here. admin.site.register(Feature)
this screenshot is from my admin panel > add Feature (myModel) and it shows nothing.
screenshot of admin panel> add Feature
Advertisement
Answer
You are using a @dataclass
way of defining your model. I’m not aware that will work … It should be:
class Feature(models.Model): name = models.CharField(max_length=100) details = models.CharField(max_length=200)
Also, have you run migrations? (I suspect not or I think you’d have seen an error here). You should run python manage.py makemigrations
followed by python manage.py migrate
when you create or amend a model definition – this is what creates/changes it in the DB, and without that there’s not a lot to display in admin anyway …
It’s possible if you ran migrate it created an “empty” model maybe, since you were missing the =
– I really don’t know exactly what this code would do without testing it.