3

I am stuck with an Error with models.DateField()

First, I did this.

models.py

from datetime import date, datetime
from django.db import models

class User(models.Model):
    uid = models.AutoField(primary_key=True)
    birthdate = models.DateField()

Then, I got,

$ python manage.py makemigrations
You are trying to add a non-nullable field 'birthdate' to user_profile without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
 1) Provide a one-off default now (will be set on all existing rows)
 2) Quit, and let me add a default in models.py

So, I did,

models.py

from datetime import date, datetime
from django.db import models

class User(models.Model):
    uid = models.AutoField(primary_key=True)
    birthdate = models.DateField(default=date.today)

Then,

$ python manage.py migrate
django.core.exceptions.ValidationError: ["'' は無効な日付形式です。YYYY-MM-DD形式にしなければなりません。"]

The error means, like "'' is invalid for formate of date. You should change to YYYY-MM-DD".

How should I change this code? Thank you.

/// additional /// If I can, I don't want to INSERT date INTO birthdate field. But it seems I have to. Can I let it blank?

birthdate = models.DateField(null=True, blank=False)

didn't work.

Python 3.5.1 Django 1.9.1

Savad KP
  • 1,625
  • 3
  • 28
  • 40
yamachan
  • 1,029
  • 2
  • 12
  • 28
  • `blank=False` means the field cannot be blank. Can you change it to `blank=True` and try again? – Shang Wang Feb 05 '16 at 14:19
  • Thank you for reply Shang. I did with birthdate = models.DateField(null=True, blank=True) and I got ValidationError again. And I did with birthdate = models.DateField(null=True, blank=True) and terminal says "1) Provide a one-off default now (will be set on all existing rows) 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) 3) Quit, and let me add a default in models.py" – yamachan Feb 05 '16 at 14:28
  • 1
    Well, if you have prompt for choices when you do `null=True, blank=True`, isn't the second option what you want(leave the field NULL)? Also, I feel like your migration might messed up because you tried to migrate many times. You should remove the migration file that responsible for this and generate again. – Shang Wang Feb 05 '16 at 14:32
  • Thank you so much. I didn't know about migration files. After I deleted files in /migrations, it worked. It took 5 hours for that, so I am very thankful to you. – yamachan Feb 05 '16 at 15:01

3 Answers3

2

Sounds like your migration files are messed up. When you do a migration, django would create a migration file that records what you did. So in short, you changed your model code many times, but you never changed your migration file, or you are creating duplicate migration files.

The following should be what you want,

birthdate = models.DateField(null=True, blank=True)

But as you noticed, cleaning up all migration files that related to this change and create a new one should solve the problem.

Shang Wang
  • 24,909
  • 20
  • 73
  • 94
  • I learned about migrations files this time. I used old Django before, so it gave me a chance to study migrations. And thank you for giving me advice many times. – yamachan Feb 05 '16 at 15:16
1

What you have tried should work:

birthdate = models.DateField(null=True, blank=False)

This allows the database to accept null values (which it does during the migration) and blank means that django will not accept empty values from forms.

Make sure to delete migrations that have been made but not applied. Also try deleting all .pyc's in your project.

knelson
  • 126
  • 1
  • 6
1

Try this,

birthdate = models.DateField(null=True, blank=False)
Savad KP
  • 1,625
  • 3
  • 28
  • 40
  • 1
    Thank you for your reply, Savad. Those worked. Finally I could solve this. I am thankful to you. – yamachan Feb 05 '16 at 15:04
  • That's not going to work. If you add the parenthesis, you are calling the function the first time model is instantiated, and django would reuse the value the first time it is called. The value would only update when you restart django server. So if you create an object tomorrow the birthdate is still today. – Shang Wang Feb 05 '16 at 15:16
  • But if you pass `date.today` without parenthesis, you are passing a function object instead of calling it, so every time you create a new user, the function is called at run time, that would guarantee to get the current `today()` return value, which should be the right thing to do. – Shang Wang Feb 05 '16 at 15:17
  • Sorry, I am not familiar with this site yet. I clicked △. Ah, I need click ✔. – yamachan Feb 05 '16 at 15:18
  • http://stackoverflow.com/questions/2771676/django-datetime-issues-default-datetime-now – Shang Wang Feb 05 '16 at 15:19
  • Ah, yeah. I must have written models.DateField(default=date.today). – yamachan Feb 05 '16 at 15:22
  • @ShangWang Thanks. Now I also learned about default in date/datetime field migrations. – Savad KP Feb 05 '16 at 15:32