Time difference in Django 1.11
I want my code to do two things:
Get datefield to give days only.
Get the difference btwn a depature_date and now.
Then this should be posted as:
2 days remaining to book.
I have been researching without any luck. Please help.
Models.py
from datetime import datetime; datetime.date; datetime.today;
datetime.now
class JoinedSafaris(models.Model):
package=models.ForeignKey(Packages)
Location=models.CharField(max_length=15, default="location")
date_of_depature=models.DateField(default=datetime.today)
today=models.DateField(default=datetime.now)
PS. When i use datetime.date or datetime.day in the fields, I get an error like this:
ValueError: Cannot serialize: <attribute 'day' of 'datetime.date' objects>
There are some values Django cannot serialize into migration files.
python django
add a comment |
I want my code to do two things:
Get datefield to give days only.
Get the difference btwn a depature_date and now.
Then this should be posted as:
2 days remaining to book.
I have been researching without any luck. Please help.
Models.py
from datetime import datetime; datetime.date; datetime.today;
datetime.now
class JoinedSafaris(models.Model):
package=models.ForeignKey(Packages)
Location=models.CharField(max_length=15, default="location")
date_of_depature=models.DateField(default=datetime.today)
today=models.DateField(default=datetime.now)
PS. When i use datetime.date or datetime.day in the fields, I get an error like this:
ValueError: Cannot serialize: <attribute 'day' of 'datetime.date' objects>
There are some values Django cannot serialize into migration files.
python django
add a comment |
I want my code to do two things:
Get datefield to give days only.
Get the difference btwn a depature_date and now.
Then this should be posted as:
2 days remaining to book.
I have been researching without any luck. Please help.
Models.py
from datetime import datetime; datetime.date; datetime.today;
datetime.now
class JoinedSafaris(models.Model):
package=models.ForeignKey(Packages)
Location=models.CharField(max_length=15, default="location")
date_of_depature=models.DateField(default=datetime.today)
today=models.DateField(default=datetime.now)
PS. When i use datetime.date or datetime.day in the fields, I get an error like this:
ValueError: Cannot serialize: <attribute 'day' of 'datetime.date' objects>
There are some values Django cannot serialize into migration files.
python django
I want my code to do two things:
Get datefield to give days only.
Get the difference btwn a depature_date and now.
Then this should be posted as:
2 days remaining to book.
I have been researching without any luck. Please help.
Models.py
from datetime import datetime; datetime.date; datetime.today;
datetime.now
class JoinedSafaris(models.Model):
package=models.ForeignKey(Packages)
Location=models.CharField(max_length=15, default="location")
date_of_depature=models.DateField(default=datetime.today)
today=models.DateField(default=datetime.now)
PS. When i use datetime.date or datetime.day in the fields, I get an error like this:
ValueError: Cannot serialize: <attribute 'day' of 'datetime.date' objects>
There are some values Django cannot serialize into migration files.
python django
python django
edited Nov 10 '18 at 19:34
vorujack
607614
607614
asked Nov 10 '18 at 18:50
Negrita DevelopersNegrita Developers
165
165
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The datetime.now
function returns the current datetime.
If you want to save the creation date of an instance, add the auto_now_add
attribute to your model.
class JoinedSafaris(models.Model):
package=models.ForeignKey(Packages)
Location=models.CharField(max_length=15, default="location")
date_of_depature=models.DateField(default=datetime.today)
today=models.DateField(auto_now_add=True)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242306%2ftime-difference-in-django-1-11%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The datetime.now
function returns the current datetime.
If you want to save the creation date of an instance, add the auto_now_add
attribute to your model.
class JoinedSafaris(models.Model):
package=models.ForeignKey(Packages)
Location=models.CharField(max_length=15, default="location")
date_of_depature=models.DateField(default=datetime.today)
today=models.DateField(auto_now_add=True)
add a comment |
The datetime.now
function returns the current datetime.
If you want to save the creation date of an instance, add the auto_now_add
attribute to your model.
class JoinedSafaris(models.Model):
package=models.ForeignKey(Packages)
Location=models.CharField(max_length=15, default="location")
date_of_depature=models.DateField(default=datetime.today)
today=models.DateField(auto_now_add=True)
add a comment |
The datetime.now
function returns the current datetime.
If you want to save the creation date of an instance, add the auto_now_add
attribute to your model.
class JoinedSafaris(models.Model):
package=models.ForeignKey(Packages)
Location=models.CharField(max_length=15, default="location")
date_of_depature=models.DateField(default=datetime.today)
today=models.DateField(auto_now_add=True)
The datetime.now
function returns the current datetime.
If you want to save the creation date of an instance, add the auto_now_add
attribute to your model.
class JoinedSafaris(models.Model):
package=models.ForeignKey(Packages)
Location=models.CharField(max_length=15, default="location")
date_of_depature=models.DateField(default=datetime.today)
today=models.DateField(auto_now_add=True)
edited Nov 10 '18 at 22:52
Avery
4915
4915
answered Nov 10 '18 at 19:04
vorujackvorujack
607614
607614
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242306%2ftime-difference-in-django-1-11%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown