My Status object is not saving in Django

2024/7/7 5:14:29

When I enter the shell, I run into the following problem:

from users.models import Status
from django.utils import timezoneStatus.objects.all()
>>> []
p = Status()
p.status_time = timezone.datetime.min
p.status_time
>>> datetime.datetime(1, 1, 1, 0, 0)
p.save()
Status.objects.all()
>>> [<Status: Status object>]
print(Status.objects.all()[0].status_time)
>>> None

I don't understand why the status_time is not saving properly. My Status model is below:

class Status(models.Model):status = models.CharField(max_length=200)status_time = models.DateTimeField('date published')def __init__(self, status = '', time=timezone.datetime.min, *args, **kwargs):self.status = statusself.status_time = timesuper(Status, self).__init__(*args, **kwargs)

Could someone please explain this behavior and possibly suggest a fix?

Answer

I told you before, you need to read the documentation and follow the tutorial. This is not the way to set a default value for a model's field. Please go through the documentation and tutorial at https://docs.djangoproject.com/en/1.5/ If you run into problems during it, come back and ask questions, but if you continue to things incorrectly you are just forming bad habits.


I realize that the original content may seem aggressive, that was not the intent. I just want to prevent you from doing things your way as opposed to the way they are designed to be done and wasting tons of your time in the process (I've done it before). That being said, I will try and demonstrate how the designers of django would most likely have wanted you to set up your model.

class Status(models.Model):status = models.CharField(max_length=200, null=True, blank=True)time = models.DateTimeField('date published', default=timezone.datetime.min)

As you can see, there is no __init__ used when creating models. You simply give it fields as properties. Each field type has several options to set things like default values, max_length, etc...

https://en.xdnf.cn/q/120016.html

Related Q&A

Too many instances are running for Django Server

When Django server gets started, I can see only one instance of Django server running in the background. But after a while, I can see multiple instances are running.Output:root@GoldenGate:~# ps |grep p…

Not able to display images from static folder using Django

This is my home.html Im not able to display the images in static/images/ folder. Although *[09/Mar/2020 15:52:09] "GET /static/images/mona.jpg HTTP/1.1" 404 1669 * is displayed in terminal.&l…

Regex to match phone number 5ABCDYYZZ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 1 year ago.Improve …

Remove an \\n\\t\\t\\t-element from list

I got the following list called "phonenumbers". I struggle to remove the elements which contain \n\t\t\t and \n\t\t\t\t. I tried "try and except"-methode and remove(\n\t\t\t\t) but …

Find all numbers in a string in Python 3 [duplicate]

This question already has answers here:How to extract numbers from a string in Python?(20 answers)Split Strings into words with multiple word boundary delimiters(31 answers)Closed 8 years ago.Newbie h…

call dictionary from one function to another

How can I call a dictionary created in one function to another?I have tried using How do I access a dictionary from a function to be used in another function? but it doesnt work for me.I have created…

How to change values in numpy array

import numpy as np a=np.array([[4,2,6],[3,6,5]]) b=np.array([3,5])I want to update the numbers in "a" which are bigger than the numbers in "b" to np.nan. If they are smaller or equa…

Why use the object oriented approach in matplotlib for visualizing data? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Set a status for a discord bot

I am trying to get my bot to have the status, m!help. I see that a lot of other bots have their help command in their status so I wanted to do that too. await bot.change_presence(activity=discord.Game(…

Can you permanently change python code by input?

Im still learning python and am currently developing an API (artificial personal assistant e.g. Siri or Cortana). I was wondering if there was a way to update code by input. For example, if I had a lis…