해결된 질문
작성
·
601
0
선생님, 안녕하세요.
장고 입문해서, 강의를 잘 듣고 있습니다.
강의 들으면서 아래와 같은 문제가 생겨서 문의 드립니다.
조언 부탁드립니다.
다음과 같이 작성하면 문제가 없습니다
from django.db import models
from django.conf import settings
# Create your models here.
class Post(models.Model):
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
message = models.TextField()
created_at = models.DateTimeField(auto_now_add=True, null=True)
updated_at = models.DateTimeField(auto_now=True, null=True)
title = models.CharField(max_length=100, db_index=True)
# slug = models.SlugField(allow_unicode=True, db_column=True, null=True) #제목과 숫자로 이루어진 url 생성시 사용
desc = models.TextField(blank=True) # 빈문자열도 허용
image = models.ImageField(blank=True)
comment_count = models.PositiveIntegerField(default=0) # 양수만 처리
tag_set = models.ManyToManyField('Tag') # 하나의 포스트는 다수의 태그
is_publish = models.BooleanField(default=False)
slug 주석해제하고 DB 마이그레이션 문제가 생깁니다.
from django.db import models
from django.conf import settings
# Create your models here.
class Post(models.Model):
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
message = models.TextField()
created_at = models.DateTimeField(auto_now_add=True, null=True)
updated_at = models.DateTimeField(auto_now=True, null=True)
title = models.CharField(max_length=100, db_index=True)
slug = models.SlugField(allow_unicode=True, db_column=True, null=True) #제목과 숫자로 이루어진 url 생성시 사용
desc = models.TextField(blank=True) # 빈문자열도 허용
image = models.ImageField(blank=True)
comment_count = models.PositiveIntegerField(default=0) # 양수만 처리
tag_set = models.ManyToManyField('Tag') # 하나의 포스트는 다수의 태그
is_publish = models.BooleanField(default=False)
그런데 slug 필드 주석 해제하고 마이그레이션 하면 다음과 같은 에러가 생깁니다.
(venv) C:\Users\kwanw\Desktop\askcompany>python manage.py migrate instagram
Operations to perform:
Apply all migrations: instagram
Running migrations:
Applying instagram.0006_post_slug...Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "C:\Users\kwanw\Desktop\askcompany\venv\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\Users\kwanw\Desktop\askcompany\venv\lib\site-packages\django\core\management\__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\kwanw\Desktop\askcompany\venv\lib\site-packages\django\core\management\base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\kwanw\Desktop\askcompany\venv\lib\site-packages\django\core\management\base.py", line 369, in execute
output = self.handle(*args, **options)
File "C:\Users\kwanw\Desktop\askcompany\venv\lib\site-packages\django\core\management\base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\kwanw\Desktop\askcompany\venv\lib\site-packages\django\core\management\commands\migrate.py", line 233, in handle
fake_initial=fake_initial,
File "C:\Users\kwanw\Desktop\askcompany\venv\lib\site-packages\django\db\migrations\executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "C:\Users\kwanw\Desktop\askcompany\venv\lib\site-packages\django\db\migrations\executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "C:\Users\kwanw\Desktop\askcompany\venv\lib\site-packages\django\db\migrations\executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Users\kwanw\Desktop\askcompany\venv\lib\site-packages\django\db\migrations\migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "C:\Users\kwanw\Desktop\askcompany\venv\lib\site-packages\django\db\migrations\operations\fields.py", line 112, in database_forwards
field,
File "C:\Users\kwanw\Desktop\askcompany\venv\lib\site-packages\django\db\backends\sqlite3\schema.py", line 328, in add_field
self._remake_table(model, create_field=field)
File "C:\Users\kwanw\Desktop\askcompany\venv\lib\site-packages\django\db\backends\sqlite3\schema.py", line 280, in _remake_table
self.create_model(new_model)
File "C:\Users\kwanw\Desktop\askcompany\venv\lib\site-packages\django\db\backends\base\schema.py", line 322, in create_model
sql, params = self.table_sql(model)
File "C:\Users\kwanw\Desktop\askcompany\venv\lib\site-packages\django\db\backends\base\schema.py", line 184, in table_sql
self.quote_name(field.column),
File "C:\Users\kwanw\Desktop\askcompany\venv\lib\site-packages\django\db\backends\base\schema.py", line 145, in quote_name
return self.connection.ops.quote_name(name)
File "C:\Users\kwanw\Desktop\askcompany\venv\lib\site-packages\django\db\backends\sqlite3\operations.py", line 164, in quote_name
if name.startswith('"') and name.endswith('"'):
AttributeError: 'bool' object has no attribute 'startswith'
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('instagram', '0005_profile'),
]
operations = [
migrations.AddField(
model_name='post',
name='slug',
field=models.SlugField(allow_unicode=True, db_column=True, null=True),
),
]
답변 1
0
안녕하세요.
slug 필드에 지정하신 db_column 인자는 db_index 이름을 잘못 입력하신 듯 합니다.
db_column은 db 컬럼명을 직접 지정하는 옵션이거든요. 그러니 문자열 타입이어야 합니다.
아래의 에러메세지를 읽어보시면, bool 객체에 startswith 속성을 참조할려는 데 없다. startswith는 str타입에서 지원하는 메서드입니다. 그러니 문자열 타입이어야하는 부분에 bool 타입 값이 지정되어서 발생하는 오류인 것이죠.
AttributeError: 'bool' object has no attribute 'startswith'
살펴보시고 댓글 남겨주세요.
화이팅입니다. ;-)
이렇게 자세하게 설명까지 해 주셔서 감사합니다. 덕분에 금방 문제도 해결하고, 에러코드 해석하는데도 많은 도움이 되었습니다.