DateField and TimeField don't come out after adding to model class
I'm quite new to Python and Django. I'm setting up a new model class and I'm able to add appointments on the admin page, but here's the strange thing:
I used to have a DateTimeFiled, which I changed to a separate DateField and a TimeField. But now I see neither my DateField nor my TimeField. Why is that, I don't understand it. I've done the migration, everything looks right.
This is my model class:
class Appointment(models.Model):
patient = models.ForeignKey(User, related_name="appointment_patient", on_delete=False)
date = models.DateField(auto_now=True)
start_time = models.TimeField(auto_now=True)
duration = models.DurationField(default=timedelta(minutes=30))
Before I made this change every property came out right, but now this happens:
python django django-models
add a comment |
I'm quite new to Python and Django. I'm setting up a new model class and I'm able to add appointments on the admin page, but here's the strange thing:
I used to have a DateTimeFiled, which I changed to a separate DateField and a TimeField. But now I see neither my DateField nor my TimeField. Why is that, I don't understand it. I've done the migration, everything looks right.
This is my model class:
class Appointment(models.Model):
patient = models.ForeignKey(User, related_name="appointment_patient", on_delete=False)
date = models.DateField(auto_now=True)
start_time = models.TimeField(auto_now=True)
duration = models.DurationField(default=timedelta(minutes=30))
Before I made this change every property came out right, but now this happens:
python django django-models
Check this question. It is because you setauto_now
in your fields.
– ivissani
Nov 22 '18 at 13:09
add a comment |
I'm quite new to Python and Django. I'm setting up a new model class and I'm able to add appointments on the admin page, but here's the strange thing:
I used to have a DateTimeFiled, which I changed to a separate DateField and a TimeField. But now I see neither my DateField nor my TimeField. Why is that, I don't understand it. I've done the migration, everything looks right.
This is my model class:
class Appointment(models.Model):
patient = models.ForeignKey(User, related_name="appointment_patient", on_delete=False)
date = models.DateField(auto_now=True)
start_time = models.TimeField(auto_now=True)
duration = models.DurationField(default=timedelta(minutes=30))
Before I made this change every property came out right, but now this happens:
python django django-models
I'm quite new to Python and Django. I'm setting up a new model class and I'm able to add appointments on the admin page, but here's the strange thing:
I used to have a DateTimeFiled, which I changed to a separate DateField and a TimeField. But now I see neither my DateField nor my TimeField. Why is that, I don't understand it. I've done the migration, everything looks right.
This is my model class:
class Appointment(models.Model):
patient = models.ForeignKey(User, related_name="appointment_patient", on_delete=False)
date = models.DateField(auto_now=True)
start_time = models.TimeField(auto_now=True)
duration = models.DurationField(default=timedelta(minutes=30))
Before I made this change every property came out right, but now this happens:
python django django-models
python django django-models
asked Nov 22 '18 at 13:05
DanDanDanDan
1201211
1201211
Check this question. It is because you setauto_now
in your fields.
– ivissani
Nov 22 '18 at 13:09
add a comment |
Check this question. It is because you setauto_now
in your fields.
– ivissani
Nov 22 '18 at 13:09
Check this question. It is because you set
auto_now
in your fields.– ivissani
Nov 22 '18 at 13:09
Check this question. It is because you set
auto_now
in your fields.– ivissani
Nov 22 '18 at 13:09
add a comment |
1 Answer
1
active
oldest
votes
auto_now
automatically updated with timezone.now()
whenever the model object is saved also its not editable. So its not necessary to display them in the template. That is why adminsite omits this field or does not show it. Please see the documentation for more details. If you want to display this field then add it readonly_fields
. For example:
class YourModelAdmin(admin.ModelAdmin):
readonly_fields=('start_time',)
Update
If you want to add default value to DateField, try like this:
import datetime
# in models
state_date = models.DateField(default=datetime.date.today) # without parenthesis
Oh I see, that makes sense. I tried it before with a default value but ran into issues, then I tried it like that. Anyway it should not be readonly. I've retried it with a default value but I do not get it how to set it. There are multiple explanations around which don't work (they might be Python 2).I've seen this line in the documentation, which I doesn't work without the import part:For DateField: default=date.today - from datetime.date.today()
. I don't understand the import part. That's not the import syntax?!? What's the import statement so thatdate.today
would work?
– DanDan
Nov 22 '18 at 13:22
It should bestate_date = models.DateField(default=datetime.date.today)
– ruddra
Nov 22 '18 at 13:37
this Python syntax is killing me! Neither works, notdefault=date.today
nordefault=datetime.date.today
. In the first case PyCharm already complains, in the latter case the migration fails:AttributeError: 'method_descriptor' object has no attribute 'today'
– DanDan
Nov 22 '18 at 14:03
1
@DanDan I do not face this issue. i am importingdatetime
only. please see my answer's update section
– ruddra
Nov 22 '18 at 14:06
That's it, now it works. Why is that? But thanks anyway!
– DanDan
Nov 22 '18 at 14:09
|
show 1 more 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%2f53431695%2fdatefield-and-timefield-dont-come-out-after-adding-to-model-class%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
auto_now
automatically updated with timezone.now()
whenever the model object is saved also its not editable. So its not necessary to display them in the template. That is why adminsite omits this field or does not show it. Please see the documentation for more details. If you want to display this field then add it readonly_fields
. For example:
class YourModelAdmin(admin.ModelAdmin):
readonly_fields=('start_time',)
Update
If you want to add default value to DateField, try like this:
import datetime
# in models
state_date = models.DateField(default=datetime.date.today) # without parenthesis
Oh I see, that makes sense. I tried it before with a default value but ran into issues, then I tried it like that. Anyway it should not be readonly. I've retried it with a default value but I do not get it how to set it. There are multiple explanations around which don't work (they might be Python 2).I've seen this line in the documentation, which I doesn't work without the import part:For DateField: default=date.today - from datetime.date.today()
. I don't understand the import part. That's not the import syntax?!? What's the import statement so thatdate.today
would work?
– DanDan
Nov 22 '18 at 13:22
It should bestate_date = models.DateField(default=datetime.date.today)
– ruddra
Nov 22 '18 at 13:37
this Python syntax is killing me! Neither works, notdefault=date.today
nordefault=datetime.date.today
. In the first case PyCharm already complains, in the latter case the migration fails:AttributeError: 'method_descriptor' object has no attribute 'today'
– DanDan
Nov 22 '18 at 14:03
1
@DanDan I do not face this issue. i am importingdatetime
only. please see my answer's update section
– ruddra
Nov 22 '18 at 14:06
That's it, now it works. Why is that? But thanks anyway!
– DanDan
Nov 22 '18 at 14:09
|
show 1 more comment
auto_now
automatically updated with timezone.now()
whenever the model object is saved also its not editable. So its not necessary to display them in the template. That is why adminsite omits this field or does not show it. Please see the documentation for more details. If you want to display this field then add it readonly_fields
. For example:
class YourModelAdmin(admin.ModelAdmin):
readonly_fields=('start_time',)
Update
If you want to add default value to DateField, try like this:
import datetime
# in models
state_date = models.DateField(default=datetime.date.today) # without parenthesis
Oh I see, that makes sense. I tried it before with a default value but ran into issues, then I tried it like that. Anyway it should not be readonly. I've retried it with a default value but I do not get it how to set it. There are multiple explanations around which don't work (they might be Python 2).I've seen this line in the documentation, which I doesn't work without the import part:For DateField: default=date.today - from datetime.date.today()
. I don't understand the import part. That's not the import syntax?!? What's the import statement so thatdate.today
would work?
– DanDan
Nov 22 '18 at 13:22
It should bestate_date = models.DateField(default=datetime.date.today)
– ruddra
Nov 22 '18 at 13:37
this Python syntax is killing me! Neither works, notdefault=date.today
nordefault=datetime.date.today
. In the first case PyCharm already complains, in the latter case the migration fails:AttributeError: 'method_descriptor' object has no attribute 'today'
– DanDan
Nov 22 '18 at 14:03
1
@DanDan I do not face this issue. i am importingdatetime
only. please see my answer's update section
– ruddra
Nov 22 '18 at 14:06
That's it, now it works. Why is that? But thanks anyway!
– DanDan
Nov 22 '18 at 14:09
|
show 1 more comment
auto_now
automatically updated with timezone.now()
whenever the model object is saved also its not editable. So its not necessary to display them in the template. That is why adminsite omits this field or does not show it. Please see the documentation for more details. If you want to display this field then add it readonly_fields
. For example:
class YourModelAdmin(admin.ModelAdmin):
readonly_fields=('start_time',)
Update
If you want to add default value to DateField, try like this:
import datetime
# in models
state_date = models.DateField(default=datetime.date.today) # without parenthesis
auto_now
automatically updated with timezone.now()
whenever the model object is saved also its not editable. So its not necessary to display them in the template. That is why adminsite omits this field or does not show it. Please see the documentation for more details. If you want to display this field then add it readonly_fields
. For example:
class YourModelAdmin(admin.ModelAdmin):
readonly_fields=('start_time',)
Update
If you want to add default value to DateField, try like this:
import datetime
# in models
state_date = models.DateField(default=datetime.date.today) # without parenthesis
edited Nov 22 '18 at 13:37
answered Nov 22 '18 at 13:12
ruddraruddra
13.3k32648
13.3k32648
Oh I see, that makes sense. I tried it before with a default value but ran into issues, then I tried it like that. Anyway it should not be readonly. I've retried it with a default value but I do not get it how to set it. There are multiple explanations around which don't work (they might be Python 2).I've seen this line in the documentation, which I doesn't work without the import part:For DateField: default=date.today - from datetime.date.today()
. I don't understand the import part. That's not the import syntax?!? What's the import statement so thatdate.today
would work?
– DanDan
Nov 22 '18 at 13:22
It should bestate_date = models.DateField(default=datetime.date.today)
– ruddra
Nov 22 '18 at 13:37
this Python syntax is killing me! Neither works, notdefault=date.today
nordefault=datetime.date.today
. In the first case PyCharm already complains, in the latter case the migration fails:AttributeError: 'method_descriptor' object has no attribute 'today'
– DanDan
Nov 22 '18 at 14:03
1
@DanDan I do not face this issue. i am importingdatetime
only. please see my answer's update section
– ruddra
Nov 22 '18 at 14:06
That's it, now it works. Why is that? But thanks anyway!
– DanDan
Nov 22 '18 at 14:09
|
show 1 more comment
Oh I see, that makes sense. I tried it before with a default value but ran into issues, then I tried it like that. Anyway it should not be readonly. I've retried it with a default value but I do not get it how to set it. There are multiple explanations around which don't work (they might be Python 2).I've seen this line in the documentation, which I doesn't work without the import part:For DateField: default=date.today - from datetime.date.today()
. I don't understand the import part. That's not the import syntax?!? What's the import statement so thatdate.today
would work?
– DanDan
Nov 22 '18 at 13:22
It should bestate_date = models.DateField(default=datetime.date.today)
– ruddra
Nov 22 '18 at 13:37
this Python syntax is killing me! Neither works, notdefault=date.today
nordefault=datetime.date.today
. In the first case PyCharm already complains, in the latter case the migration fails:AttributeError: 'method_descriptor' object has no attribute 'today'
– DanDan
Nov 22 '18 at 14:03
1
@DanDan I do not face this issue. i am importingdatetime
only. please see my answer's update section
– ruddra
Nov 22 '18 at 14:06
That's it, now it works. Why is that? But thanks anyway!
– DanDan
Nov 22 '18 at 14:09
Oh I see, that makes sense. I tried it before with a default value but ran into issues, then I tried it like that. Anyway it should not be readonly. I've retried it with a default value but I do not get it how to set it. There are multiple explanations around which don't work (they might be Python 2).I've seen this line in the documentation, which I doesn't work without the import part:
For DateField: default=date.today - from datetime.date.today()
. I don't understand the import part. That's not the import syntax?!? What's the import statement so that date.today
would work?– DanDan
Nov 22 '18 at 13:22
Oh I see, that makes sense. I tried it before with a default value but ran into issues, then I tried it like that. Anyway it should not be readonly. I've retried it with a default value but I do not get it how to set it. There are multiple explanations around which don't work (they might be Python 2).I've seen this line in the documentation, which I doesn't work without the import part:
For DateField: default=date.today - from datetime.date.today()
. I don't understand the import part. That's not the import syntax?!? What's the import statement so that date.today
would work?– DanDan
Nov 22 '18 at 13:22
It should be
state_date = models.DateField(default=datetime.date.today)
– ruddra
Nov 22 '18 at 13:37
It should be
state_date = models.DateField(default=datetime.date.today)
– ruddra
Nov 22 '18 at 13:37
this Python syntax is killing me! Neither works, not
default=date.today
nor default=datetime.date.today
. In the first case PyCharm already complains, in the latter case the migration fails: AttributeError: 'method_descriptor' object has no attribute 'today'
– DanDan
Nov 22 '18 at 14:03
this Python syntax is killing me! Neither works, not
default=date.today
nor default=datetime.date.today
. In the first case PyCharm already complains, in the latter case the migration fails: AttributeError: 'method_descriptor' object has no attribute 'today'
– DanDan
Nov 22 '18 at 14:03
1
1
@DanDan I do not face this issue. i am importing
datetime
only. please see my answer's update section– ruddra
Nov 22 '18 at 14:06
@DanDan I do not face this issue. i am importing
datetime
only. please see my answer's update section– ruddra
Nov 22 '18 at 14:06
That's it, now it works. Why is that? But thanks anyway!
– DanDan
Nov 22 '18 at 14:09
That's it, now it works. Why is that? But thanks anyway!
– DanDan
Nov 22 '18 at 14:09
|
show 1 more 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%2f53431695%2fdatefield-and-timefield-dont-come-out-after-adding-to-model-class%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
Check this question. It is because you set
auto_now
in your fields.– ivissani
Nov 22 '18 at 13:09