DateField and TimeField don't come out after adding to model class












0















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:



enter image description here










share|improve this question























  • Check this question. It is because you set auto_now in your fields.

    – ivissani
    Nov 22 '18 at 13:09
















0















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:



enter image description here










share|improve this question























  • Check this question. It is because you set auto_now in your fields.

    – ivissani
    Nov 22 '18 at 13:09














0












0








0








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:



enter image description here










share|improve this question














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:



enter image description here







python django django-models






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 13:05









DanDanDanDan

1201211




1201211













  • 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

















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












1 Answer
1






active

oldest

votes


















2














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





share|improve this answer


























  • 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











  • 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





    @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













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
});


}
});














draft saved

draft discarded


















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









2














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





share|improve this answer


























  • 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











  • 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





    @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


















2














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





share|improve this answer


























  • 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











  • 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





    @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
















2












2








2







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





share|improve this answer















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






share|improve this answer














share|improve this answer



share|improve this answer








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 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











  • 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





    @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





















  • 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











  • 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





    @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



















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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen