質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Django

DjangoはPythonで書かれた、オープンソースウェブアプリケーションのフレームワークです。複雑なデータベースを扱うウェブサイトを開発する際に必要な労力を減らす為にデザインされました。

Model

MVCモデルの一部であるModelはアプリケーションで扱うデータとその動作を管理するために扱います。

Webサイト

一つのドメイン上に存在するWebページの集合体をWebサイトと呼びます。

データベース

データベースとは、データの集合体を指します。また、そのデータの集合体の共用を可能にするシステムの意味を含めます

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

1249閲覧

【django】migrateの際に起こるエラーを解決したい

kabos0809

総合スコア3

Django

DjangoはPythonで書かれた、オープンソースウェブアプリケーションのフレームワークです。複雑なデータベースを扱うウェブサイトを開発する際に必要な労力を減らす為にデザインされました。

Model

MVCモデルの一部であるModelはアプリケーションで扱うデータとその動作を管理するために扱います。

Webサイト

一つのドメイン上に存在するWebページの集合体をWebサイトと呼びます。

データベース

データベースとは、データの集合体を指します。また、そのデータの集合体の共用を可能にするシステムの意味を含めます

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2022/01/03 14:17

djangoでカスタムユーザーモデルを作り、idの代わりにuuidを使いたかったのでuuidに変更しました。
しかし、いざmigrateしようとしたところ、以下のようなエラーが発生し非常に困っております。

File "C:\Users\hiyam\anaconda3\lib\site-packages\django\db\models\options.py", line 565, in get_field return self.fields_map[field_name] KeyError: 'id' During handling of the above exception, another exception occurred: 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\hiyam\anaconda3\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line utility.execute() File "C:\Users\hiyam\anaconda3\lib\site-packages\django\core\management\__init__.py", line 375, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "C:\Users\hiyam\anaconda3\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv self.execute(*args, **cmd_options) File "C:\Users\hiyam\anaconda3\lib\site-packages\django\core\management\base.py", line 364, in execute output = self.handle(*args, **options) File "C:\Users\hiyam\anaconda3\lib\site-packages\django\core\management\base.py", line 83, in wrapped res = handle_func(*args, **kwargs) File "C:\Users\hiyam\anaconda3\lib\site-packages\django\core\management\commands\migrate.py", line 232, in handle post_migrate_state = executor.migrate( File "C:\Users\hiyam\anaconda3\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\hiyam\anaconda3\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\hiyam\anaconda3\lib\site-packages\django\db\migrations\executor.py", line 245, in apply_migration state = migration.apply(state, schema_editor) File "C:\Users\hiyam\anaconda3\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\hiyam\anaconda3\lib\site-packages\django\db\migrations\operations\fields.py", line 249, in database_forwards schema_editor.alter_field(from_model, from_field, to_field) File "C:\Users\hiyam\anaconda3\lib\site-packages\django\db\backends\sqlite3\schema.py", line 137, in alter_field super().alter_field(model, old_field, new_field, strict=strict) File "C:\Users\hiyam\anaconda3\lib\site-packages\django\db\backends\base\schema.py", line 507, in alter_field new_db_params = new_field.db_parameters(connection=self.connection) File "C:\Users\hiyam\anaconda3\lib\site-packages\django\db\models\fields\related.py", line 966, in db_parameters return {"type": self.db_type(connection), "check": self.db_check(connection)} File "C:\Users\hiyam\anaconda3\lib\site-packages\django\db\models\fields\related.py", line 963, in db_type return self.target_field.rel_db_type(connection=connection) File "C:\Users\hiyam\anaconda3\lib\site-packages\django\db\models\fields\related.py", line 878, in target_field return self.foreign_related_fields[0] File "C:\Users\hiyam\anaconda3\lib\site-packages\django\db\models\fields\related.py", line 632, in foreign_related_fields return tuple(rhs_field for lhs_field, rhs_field in self.related_fields if rhs_field) File "C:\Users\hiyam\anaconda3\lib\site-packages\django\db\models\fields\related.py", line 619, in related_fields self._related_fields = self.resolve_related_fields() File "C:\Users\hiyam\anaconda3\lib\site-packages\django\db\models\fields\related.py", line 612, in resolve_related_fields else self.remote_field.model._meta.get_field(to_field_name)) File "C:\Users\hiyam\anaconda3\lib\site-packages\django\db\models\options.py", line 567, in get_field raise FieldDoesNotExist("%s has no field named '%s'" % (self.object_name, field_name)) django.core.exceptions.FieldDoesNotExist: CustomUser has no field named 'id'

ここで書かれているCustomUserは私が作成したmodels.py内のclassだと思われます。

models

1class CustomUser(AbstractUser, PermissionsMixin): 2 username_validator = UnicodeUsernameValidator() 3 4 uuid = models.UUIDField(default=uuid4, primary_key=True, editable=False) 5 username = models.CharField(_("username"), max_length=30, validators=[username_validator], blank=False) 6 user_id = models.CharField(_("user_id"), max_length=30, unique=True) #※このuser_idはtwitterの@から始まるものと同様のものです。 7 email = models.EmailField(_("email_address"), unique=True) 8 is_staff = models.BooleanField(_("staff status"), default=False) 9 is_superuser = models.BooleanField(_("superuser status"), default=False) 10 date_joined = models.DateTimeField(_("date joined"), default=timezone.now) 11 playfield = models.CharField(_("play field"), max_length=30, blank=True) 12 rank = models.CharField(_("rank"), max_length=30, blank=False) 13 twitter_id = models.CharField(_("twitter id"), max_length=100, blank=True) 14 Youtube_url = models.CharField(_("YouTube CHANNEL"), max_length=100, blank=True) 15 discord_id = models.CharField(_("discord"), max_length=100, blank=True) 16 17 objects = UserManager() 18 USERNAME_FIELD = "user_id" 19 EMAIL_FIELD = "email" 20 REQUIRED_FIELDS = ['username'] 21 22 class Meta: 23 verbose_name = _("user") 24 verbose_name_plural = _("users") 25 26 def clean(self): 27 super().clean() 28 self.email = self.__class__.objects.normalize_email(self.email) 29 30 def email_user(self, subject, message, from_email=None, **kwargs): 31 send_mail(subject, message, from_email, [self.email], **kwargs) 32 33 def __str__(self): 34 return self.username 35 36class UserManager(BaseUserManager): 37 use_in_migrations = True 38 39 def _create_user(self, username, email, user_id, password, **extra_fields): 40 if not user_id: 41 raise ValueError('ユーザーidは必須です。') 42 if not email: 43 raise ValueError('Emailは必須です。') 44 email = self.normalize_email(email) 45 username = self.model.normalize_uername(username) 46 user = self.model(username=username, email=email, user_id=user_id, **extra_fields) 47 user.set_password(password) 48 user.save(using=self.db) 49 return user 50 51 def create_user(self, username, email, password=None, **extra_fields): 52 extra_fields.setdefault('is_staff', False) 53 extra_fields.setdefault('is_superuser', False) 54 return self._create_user(email, password, **extra_fields) 55 56 def create_superuser(self, username, email, password, **extra_fields): 57 extra_fields.setdefault('isstaff', True) 58 extra_fields.setdefault('is_superuser', True) 59 if extra_fields.get('is_staff') is not True: 60 raise ValueError('スタッフ権限を持っている必要があります。') 61 if extra_fields.get('is_superuser') is not True: 62 raise ValueError('スーパーユーザー権限を持っている必要があります。') 63 return self._create_user(username, email, password, **extra_fields)

CustomUserとUserManagerは以上のようになっています。
似たようなエラーについての対処法は調べて試してみましたが、全く改善いたしませんでした。
有識者の方どうかお助け願います。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

djangoのupdateを行ったところエラーが解決いたしました。

投稿2022/01/03 18:16

kabos0809

総合スコア3

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問