Djangoベース-モデルを作成し、Adminを設定します.
🌈 モデルを作成してAdmin Siteを設定する
🔥 モデルの作成
🔥 Admin設定
1.モデルの作成
1) makemigrations & migrate
from django.db import models
# Create your models here.
class Notice(models.Model):
title = models.CharField(max_length=100)
likeCount = models.IntegerField()
viewCount = models.IntegerField()
contents = models.TextField()
1) 🔍 python manage.py makemigrations
2) 🔍 python manage.py migrate
# Generated by Django 3.2 on 2021-04-15 06:06
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Notice',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=100)),
('likeCount', models.IntegerField()),
('viewCount', models.IntegerField()),
('contents', models.TextField()),
],
),
]
2.Admin設定
1) admin.site.register()
from django.contrib import admin
from .models import Notice # 👈 models.py에서 Notice를 가져옴
# Register your models here.
admin.site.register(Notice) # 👈 Notice 클래스를 admin에 등록
2) verbose_name & verbose_name_plural
from django.db import models
# Create your models here.
class Notice(models.Model):
title = models.CharField(max_length=100, verbose_name='제목')
likeCount = models.IntegerField(verbose_name='추천수')
viewCount = models.IntegerField(verbose_name='조회수')
contents = models.TextField(verbose_name='내용')
class Meta:
db_table = 'main_board'
verbose_name = '게시글'
verbose_name_plural = '게시글'
3)strメソッド
__str__
メソッドによって各オブジェクトの名前を返すことができる. from django.db import models
# Create your models here.
class Notice(models.Model):
title = models.CharField(max_length=100, verbose_name='제목')
likeCount = models.IntegerField(verbose_name='추천수')
viewCount = models.IntegerField(verbose_name='조회수')
contents = models.TextField(verbose_name='내용')
def __str__(self): # admin에서 표시될 user 필드 정보 설정
return self.title
class Meta:
db_table = 'main_board'
verbose_name = '게시글'
verbose_name_plural = '게시글'
4) list_display & list_display_links
adminサイトを介して
from django.contrib import admin
from .models import Notice
# Register your models here.
@admin.register(Notice)
class NoticeAdmin(admin.ModelAdmin):
list_display = ['title', 'likeCount', 'viewCount']
list_display_links = ['title', 'likeCount', 'viewCount']
Reference
この問題について(Djangoベース-モデルを作成し、Adminを設定します.), 我々は、より多くの情報をここで見つけました https://velog.io/@jewon119/Django-기초-모델Model-관리자adminテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol