0

I have the following model:

class Machine(models.Model):
    name = models.CharField(max_length=255, primary_key=True)
    user = models.CharField(max_length=255)
    mail = models.CharField(max_length=255)
    datetime = models.DateTimeField(blank=True, null=True, default="")
    licenses = models.CharField(max_length=10000, blank=True, null=True, default="")

as you see above I want to allow datetime and licenses to be nothing.

I want to create a new instance of this model in my view with the following code:

Machine.objects.create(name=machine_name, user=user_name, mail=user_mail)

But I get

ValidationError at /update
[u"'' value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."]

At the same time the following line works fine if run in DB directly:

INSERT INTO `xxx`.`xxx` (`name`, `user`, `mail`) VALUES ('a', 'b', 'c');

I don't understand why django wants me to enter the datetime-elements even if I have specified that null is ok.

0

2 Answers 2

2

Remove default="" for datetime field.

datetime = models.DateTimeField(blank=True, null=True,)

Even though you have specified blank=True and null=True django will try to set default value when nothing specified. But as default is not in expected format, that causes the problem.

1
  • 1
    Thank you! I really missed this.
    – theAlse
    Mar 28, 2014 at 8:44
1

You should remove default = "" from your datetime field . if you want to fill current time into datetime filed while making create entry in db you can use

auto_now_add=True

for more info you can go to django doc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.