hyeonga_code
파이선 웹구축_장고_07_테이블 생성_터미널 본문
- 테이블 생성
- 03_앱생성
1. <config> > 'settings.py'
'settings.py'
=====
1
2
3
4
5
6
7
8
9
|
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'앱이름.apps.앱이름_첫글자대문자 + Config'
]
|
cs |
2. <앱이름> > 'models.py'
'models.py'
=====
1
2
3
4
5
6
|
from django.db import models
class 클래스이름(models.Model):
숫자변수 = models.IntegerField()
문자변수 = models.CharField(max_length=100)
문자열변수 = models.TextField()
|
cs |
3. 테이블 변경 사항 확인
@ python manage.py makemigrations
=====
'''
C:\Users\hyeon\mysite\05_DB> python manage.py migrate
Migrations for '앱이름':
앱이름\migrations\0001_initial.py
- Create model 클래스이름
'''
>> <앱이름> > <migrations> > '0001_initial.py' 생성됨
'0001_initial.py'
=====
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# Generated by Django 4.1.6 on 2023-02-14 05:37
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='클래스이름',
fields=[
('id', models.BigAutoField(auto_created=True,
primary_key=True, serialize=False, verbose_name='ID')),
('숫자변수', models.IntegerField()),
('문자변수', models.CharField(max_length=100)),
('문자열변수', models.TextField()),
],
),
]
|
cs |
4. 테이블 생성
@ python manage.py migrate
'터미널'
=====
'''
C:\Users\hyeon\mysite\05_DB> python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions, 앱이름
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying auth.0010_alter_group_name_max_length... OK
Applying auth.0011_update_proxy_permissions... OK
Applying auth.0012_alter_user_first_name_max_length... OK
Applying sessions.0001_initial... OK
Applying 앱이름.0001_initial... OK
'''
5. 테이블 채우기
@ python manage.py shell
@ from 앱이름.models import 클래스이름
@ '단축어'.save() _ 바로바로 저장해야 함
@ '클래스이름'.objects.all() _ 전체조회
@ '클래스이름'.objects.get(필드) _ 상세조회
'터미널'
=====
'''
C:\Users\hyeon\mysite\05_DB> python manage.py shell
Python 3.11.1 (tags/v3.11.1:a7a450f, Dec 6 2022, 19:58:39) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from 앱이름.models import 클래스이름
>>> 단축어 = 클래스이름(숫자변수=123, 문자변수='문자', 문자열변수='문장입니다. 확인해주세요,')
>>> 단축어.save()
>>> s = 클래스이름(숫자변수=456,문자변수='문자',문자열변수='띄어쓰기도 가능합니다.')
>>> s.save()
>>> 전체조회=클래스이름.objects.all()
>>> 전체조회
<QuerySet [<클래스이름: 클래스이름 object (1)>, <클래스이름: 클래스이름 object (2)>]>
>>> 상세조회=클래스이름.objects.get(숫자변수=123)
>>> 상세조회
<클래스이름: 클래스이름 object (1)>
>>> 상세조회2=클래스이름.objects.get(문자변수='문자')
'''
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Users\hyeon\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\hyeon\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\query.py", line 653, in get
raise self.model.MultipleObjectsReturned(
앱이름.models.클래스이름.MultipleObjectsReturned: get() returned more than one 클래스이름 -- it returned 2!
---------------------------------------------------------------------------
'''
#_ 같은 속성이 2개 이상일 경우 오류
>>> for i in 전체조회:
... i.숫자변수, i.문자변수, i.문자열변수
...
(123, '문자', '문장입니다. 확인해주세요,')
(456, '문자', '띄어쓰기도 가능합니다.')
>>> 메서드1=클래스이름.objects.get(숫자변수=123)
>>> 메서드1.문자변수='변경'
>>> 메서드1.save()
>>> 메서드1.숫자변수, 메서드1.문자변수, 메서드1.문자열변수
(123, '변경', '문장입니다. 확인해주세요,')
'''
'Python_Django' 카테고리의 다른 글
파이선 웹구축_장고_09_Admin생성 (0) | 2023.06.01 |
---|---|
파이선 웹구축_장고_08_테이블 생성_페이지 (0) | 2023.05.31 |
파이선 웹구축_장고_06_URL_분리 (0) | 2023.05.29 |
파이선 웹구축_장고_05_URL_Render (0) | 2023.05.28 |
파이선 웹구축_장고_04_URL_글씨 출력 (0) | 2023.05.27 |