Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Altered Django model. I provided a one-off default = datetime.date for DateField. Found that it's invalid. Removed the field and still can't migrate

Hi here's the specific line that I added in models.py

billingMonth2 = models.DateTimeField()

This is what happened in makemigrations, I figured I added a non nullable field so I tried to define a default value:

    You are trying to add a non-nullable field 'billingMonth' to bills 
    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
    Select an option: 1
    Please enter the default value now, as valid Python
    The datetime and django.utils.timezone modules are available, so you can do 
    e.g. timezone.now()
    >>> datetime.date
    Migrations for 'bills':
    0007_auto_20160609_1400.py:
    - Change Meta options on bills
    - Add field billingMonth to bills

I found out that datetime.date was invalid and came out with an error

    ....File "/home/jrbenriquez/Envs/env1/local/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 197, in effective_default
default = field.get_default()
    File "/home/jrbenriquez/Envs/env1/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 797, in get_default
return self.default()
    TypeError: Required argument 'year' (pos 1) not found

So I just removed the billingMonth2 field hoping it would go back to normal and also setting a default value as

    `billingMonth2 = models.DateTimeField(default='Test',null=True,blank=True)`

Both still gives the same error. It seems to me I have changed something within the meta of the model in some other file or in the database and that I have to delete the default value "datetime.date" somewhere

I hope someone could help me here. Thanks!

UPDATE: Here's 0007_auto20160609_1400.py

    # -*- coding: utf-8 -*-
    # Generated by Django 1.9.6 on 2016-06-09 14:00
    from __future__ import unicode_literals

    from django.db import migrations, models
    import django.utils.datetime_safe


    class Migration(migrations.Migration): 

        dependencies = [
            ('bills', '0006_bills_duedate'),
        ]

        operations = [
            migrations.AlterModelOptions(
                name='bills',
                options={'ordering': ('-id', '-status'),     'verbose_name_plural': 'Bills'},
             ),
            migrations.AddField(
                model_name='bills',
                name='billingMonth',
                field=models.DateField(default=django.utils.datetime_safe.date),
                preserve_default=False,
             ),
]
like image 386
JR Enriquez Avatar asked Feb 06 '23 20:02

JR Enriquez


2 Answers

Please enter the default value now, as valid Python
The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now()
>>> datetime.date

I guess it is asking for the return value of the method, not the method itself. So its datetime.date() with the () at the end.

Also, in your model's field declaration, you can set the current date as a default, if that's what you tried to do

billingMonth2 = models.DateTimeField(default=datetime.date, null=True, blank=True)

There you give it the method itself, without the (), so that the time is taken when a new row is created, not only when the model is first defined.

like image 117
C14L Avatar answered Feb 09 '23 10:02

C14L


During Makemigrations use: datetime.date.today() for fill DB or you'll get "TypeError: function missing required argument 'year' (pos 1)".

like image 41
Iuli Hardt Avatar answered Feb 09 '23 11:02

Iuli Hardt