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

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

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

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

Model

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

Python

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

Q&A

解決済

1回答

1759閲覧

djangoのnull=Falseで必須項目に設定するとエラーが出る

seyu0930

総合スコア20

Django

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

Model

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

Python

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

0グッド

0クリップ

投稿2021/09/21 02:54

編集2021/09/21 02:59

djangoのカレンダーアプリを作ろうとしているのですが、予定と予定時間を必須項目にしたいです。
なのでnullとblankをFalseにしているのですが、以下のようなエラーが出てnull=Trueにしないとうまく行きません。

python

1You are trying to change the nullable field 'schedule' on calender to non-nullable without a default; we can't do that (the database needs something to populate existing rows). 2Please select a fix: 3 1) Provide a one-off default now (will be set on all existing rows with a null value for this column) 4 2) Ignore for now, and let me handle existing rows with NULL myself (e.g. because you added a RunPython or RunSQL operation to handle NULL values in a previous data migration) 5 3) Quit, and let me add a default in models.py 6Select an option:

scheduleのnullをTrueにした場合のエラー

python

1You are trying to change the nullable field 'set_date' on calender to non-nullable without a default; we can't do that (the database needs something to populate existing rows). 2Please select a fix: 3 1) Provide a one-off default now (will be set on all existing rows with a null value for this column) 4 2) Ignore for now, and let me handle existing rows with NULL myself (e.g. because you added a RunPython or RunSQL operation to handle NULL values in a previous data migration) 5 3) Quit, and let me add a default in models.py 6Select an option:

models.py

python

1from django.db import models 2from accounts.models import CustomUser 3 4# Create your models here. 5class Person(models.Model): 6 user = models.ForeignKey(CustomUser, verbose_name="ユーザー", on_delete=models.PROTECT) 7 username = models.CharField(verbose_name="ユーザーネーム", max_length=15) 8 birthyear = models.IntegerField(verbose_name="生まれた年") 9 birthmonth = models.IntegerField(verbose_name="生まれた月") 10 birthday = models.IntegerField(verbose_name="生まれた日") 11 12 def getbirthday(self): 13 return str(self.birthyear) + "年" + str(self.birthmonth) + "月" + str(self.birthday) + "日" 14 15class Friends(models.Model): 16 user = models.ForeignKey(CustomUser, verbose_name="ユーザー", on_delete=models.PROTECT) 17 18 19class Calender(models.Model): 20 user = models.ForeignKey(CustomUser, verbose_name="ユーザー", on_delete=models.PROTECT) 21 schedule = models.CharField(verbose_name="予定", max_length=30, #null=Trueを入れるとエラーは解消) 22 set_date = models.DateField("予定日", #null=Trueを入れるとエラーは解消) 23 24 def getScheduleTime(self): 25 return self.set_date 26 27 class Meta: 28 verbose_name_plural = "予定表" 29 30 def __str__(self): 31 return self.schedule

null=Trueでもblank=Falseなら必須項目になるのですがこちらのサイトを読んでnull=Falseにできないのかなと思いました。よろしくお願いします。

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

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

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

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

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

guest

回答1

0

ベストアンサー

エラーメッセージのとおりです。

いったんNULLありで作ったテーブルをNULLなしに切り替えるには、「すでにNULLとして入ってしまったデータをどうするのか」という問題が伴います。

1~3、どれが現状に合うのか選択肢を読んで考えてみましょう。

投稿2021/09/21 03:46

maisumakun

総合スコア145208

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

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

seyu0930

2021/09/22 00:52

なるほど!ありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問