samedi 29 novembre 2014

python - Django, créant une nouvelle instance de modèle de vue lève ValidationError - Stack Overflow


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.




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.




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



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.



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.



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


0 commentaires:

Enregistrer un commentaire