0

I know the title doesn't make any sense, but I just want to know how to insert an empty string or '' in a CharField made in Django? As far as I know by default, fields in Django are not nullable. Checking data in PGAdmin, I can see that most of the CharFields have '' as value. I wonder how is this possible? If I run

  cur_p.execute("""
                INSERT INTO "table"name" (address, school_address, sports ) VALUES ('', '', '')

I will get psycopg2.IntegrityError: null value in column "created" violates not-null constraint

Also I noticed that some fields have blank=True in them. Doesn't it contradict if you have a not nullable field then you declared blank=True?

tango ward
  • 307
  • 1
  • 4
  • 18
  • please, give the code of CharField of your model. – Sergey Miletskiy May 04 '18 at 08:10
  • `address = models.CharField(max_length=250) school_adress = models.CharField(max_length=250) sports = models.CharField(max_length=50)` – tango ward May 04 '18 at 08:13
  • @tangoward add `null=True` to the fields you want to be able to set null, what does it matter if you have two possible values? it's your desired outcome. – King Reload May 04 '18 at 13:08
  • 1
    @tangoward if you really want to specific explanation of what `blank=True` and `null=True` does then go to this answer: https://stackoverflow.com/a/8609425/7707749 – King Reload May 04 '18 at 13:16

1 Answers1

0

Looking at the documentation, you can use null=True in your field definition to allow postgres to store null values for the column.

arikan
  • 893
  • 9
  • 18
  • Yeah that's what I thought but these fields are CharField. The documentation says `avoid using null on string-based fields such as CharField and TextField. If a string-based field has null=True, that means it has two possible values for “no data”: NULL, and the empty string.` – tango ward May 04 '18 at 08:22
  • I think this is the best explanation on my second question about having `blank=True` https://simpleisbetterthancomplex.com/tips/2016/07/25/django-tip-8-blank-or-null.html I still don't know how to insert an empty string in a `null=False` CharField – tango ward May 04 '18 at 08:34