starting afresh

68 views
Skip to first unread message

Alex Kleider

unread,
Sep 22, 2015, 8:21:23 PM9/22/15
to django...@googlegroups.com
Although at the intermediate level with regard to Python,
I'm just beginning with Django, running Ubuntu 14.4LTS
in a venv established by
$ virtualenv -p python3 venv

I set up a journal app with a models.py file and then
changed some of the fields. Now I can't seem to get rid of
the following warning:
WARNINGS:
journal.Journal.date: (fields.W161) Fixed default value provided.
HINT: It seems you set a fixed date / time / datetime value as default
for this field. This may not be what you want. If you want to have the
current date as default, use `django.utils.timezone.now`

I deleted the db.sqlite3 file thinking that would allow me to start
fresh but somehow django remembers what was there before although it's
not there now:
file journal/models.py:
from django.db import models
from django.utils import timezone

class Journal(models.Model):
"""
Each instance represents a journal entry to be stored in the
Journal table. The automatically created auto-incrementing
primary key serves as the entry number.
"""
date = models.DateTimeField(default=timezone.now())
user = models.CharField(max_length=24)
description = models.CharField(max_length=256)
def __str__(self):
ret = [" #{:0>3} on {:<12} by {}."
.format(self.id,
self.date.strftime('%Y-%m-%d %H:%M'),
self.user)]
for line in self.description.split('\n'):
ret.append(" {}".format(line))
return '\n'.join(ret)

Any advice would be appreciated.
Alex

Nikolas Stevenson-Molnar

unread,
Sep 22, 2015, 8:30:44 PM9/22/15
to 'Tom Evans' via Django users
It's telling you that your default "date" value will be the same for every instance of "Journal" you create (essentially it'll be the time when the migration is created), when you probably intend it to be the time each Journal entry is created.

Try changing default=timezone.now() to default=timezone.now (without the parens). That should resolve the warning and yield more desirable behavior. Even better, remove the default and add auto_now_add=True (https://docs.djangoproject.com/en/1.8/ref/models/fields/#django.db.models.DateField.auto_now_add)

_Nik
ret = [" #{:0>3} on {:<12} by="" {}."="">

.format(self.id,
self.date.strftime('%Y-%m-%d %H:%M'),
self.user)]
for line in self.description.split('\n'):
ret.append(" {}".format(line))
return '\n'.join(ret)

Any advice would be appreciated.
Alex

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/25bfa6241296e00dca1e76be74a85e04%40sonic.net.
For more options, visit https://groups.google.com/d/optout.

Alex Kleider

unread,
Sep 22, 2015, 8:55:35 PM9/22/15
to django...@googlegroups.com
On 2015-09-22 17:29, Nikolas Stevenson-Molnar wrote:
> It's telling you that your default "date" value will be the same for
> every instance of "Journal" you create (essentially it'll be the time
> when the migration is created), when you probably intend it to be the
> time each Journal entry is created.
>
> Try changing default=timezone.now() to default=timezone.now (without
> the parens). That should resolve the warning and yield more desirable
> behavior. Even better, remove the default and add
> auto_now_add=True (https://docs.djangoproject.com/en/1.8/ref/models/fields/#django.db.models.DateField.auto_now_add)
>

Thank you,_Nik.
That did indeed resolve this issue and I am grateful to you.

Now I have another: I'm unable to make django forget what I've done
before!

I still get the following error:
"""
(venv)alex@x301:~/Py/debk$ ./manage.py makemigrations
Did you rename lineitems.acount to lineitems.dc_type (a CharField)?
[y/N] y
You are trying to add a non-nullable field 'acnt_number' to lineitems
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:
"""
As mentioned before, I deleted my data base but the error persists

Here's journal/models.py currently:
from django.db import models

ACCOUNT_CODE_LENGTH = 4

class Journal(models.Model):
"""
Each instance represents a journal entry to be stored in the
Journal table. The automatically created auto-incrementing
primary key serves as the entry number.
"""
date = models.DateTimeField(auto_now_add=True)
user = models.CharField(max_length=24)
description = models.CharField(max_length=256)
def __str__(self):
ret = [" #{:0>3} on {:<12} by {}."
.format(self.id,
self.date.strftime('%Y-%m-%d %H:%M'),
self.user)]
for line in self.description.split('\n'):
ret.append(" {}".format(line))
return '\n'.join(ret)

class LineItems(models.Model):
entry = models.ForeignKey(Journal)
acnt_number = models.CharField(max_length=ACCOUNT_CODE_LENGTH)
dc_type = models.CharField(max_length=1) # D(ebit or C(redit
amount = models.IntegerField() # Pennies i.e. $nn.dd * 100
def __str__(self):
if self.dc_type.upper() == 'D':
return ("{:>8}:{:>10,.2f}Dr"
.format(self.acnt_number, self.amount/100.0))
elif self.dc_type.upper() == 'C':
return ("{:>8}:{:>20,.2f}Cr"
.format(self.acnt_number, self.amount/100.0))
else:
return ("Huston, we have a problem!")


How does Django even know what was in this file before, let alone why
should it now care?


James Schneider

unread,
Sep 22, 2015, 9:13:39 PM9/22/15
to django...@googlegroups.com


> Now I have another: I'm unable to make django forget what I've done before!
>
> I still get the following error:
> """
>  (venv)alex@x301:~/Py/debk$ ./manage.py makemigrations
> Did you rename lineitems.acount to lineitems.dc_type (a CharField)? [y/N] y
> You are trying to add a non-nullable field 'acnt_number' to lineitems 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:
> """
> As mentioned before, I deleted my data base but the error persists
>

*snip*

> How does Django even know what was in this file before, let alone why should it now care?
>

Did you delete the previously created migration files? Django won't ask those questions unless previous migration files exist. If you're starting with a fresh database, you probably should.

-James

Alex Kleider

unread,
Sep 22, 2015, 9:59:40 PM9/22/15
to django...@googlegroups.com
On 2015-09-22 18:12, James Schneider wrote:


> Did you delete the previously created migration files? Django won't ask
> those questions unless previous migration files exist. If you're
> starting
> with a fresh database, you probably should.
>
> -James

Ah, ha! That must be it. Much appreciate the tip.
...but the problem is I can't find the migration files in order to
delete them.
Where are they?
A google search didn't get me far.
Thanks, Alex

James Schneider

unread,
Sep 22, 2015, 10:04:44 PM9/22/15
to django...@googlegroups.com

There should be a 'migrations' folder that was created inside of your app. You can whack the whole folder, it will be created next time you run 'makemigrations'.

-James

Alex Kleider

unread,
Sep 23, 2015, 2:04:18 AM9/23/15
to django...@googlegroups.com
On 2015-09-22 19:04, James Schneider wrote:
>>> Did you delete the previously created migration files?
.............
> There should be a 'migrations' folder that was created inside of your
> app.
> You can whack the whole folder, it will be created next time you run
> 'makemigrations'.

Indeed, it is there! (I could have sworn that I had checked but
I must have only looked in the project folder and thought I had
looked in both it and the app folder.
Thank you again and sorry to have bothered.
cheers,
Alex
Reply all
Reply to author
Forward
0 new messages