Django错误:datetime.datetime

时间:2016-03-05 11:19:30

标签: django django-models django-admin

当我执行python manage.py migrate时,屏幕上出现此错误:

File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 146, in column
sql
default_value = self.effective_default(field)

File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 211, in effect
ive_default
default = field.get_db_prep_save(default, self.connection)

File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init_.py", line 710, in get_
db_prep_save
prepared=False)

File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/init.py", line 2003, in get
db_prep_value
value = self.get_prep_value(value)

File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init_.py", line 2010, in get
_prep_value
if value and ':' in value:
TypeError: argument of type 'datetime.datetime' is not iterable

我尝试执行python manage.py syncdb,但出现了同样的错误。 可能是什么问题?

Models.py了解更多信息:

  from django.db import models
  import datetime
  from django.utils import timezone

 class Machine(models.Model):
   name = models.CharField(max_length=20, unique=True)
   ins_date = models.DateField(default=timezone.now)
   mod_date = models.DateField(default=timezone.now)
   nagios_name = models.CharField(max_length=20)
   dns = models.CharField(max_length=30)
   ip_int = models.GenericIPAddressField(null=True, blank=True)
   ip_ext = models.GenericIPAddressField()
   vlan = models.IntegerField(null=True, blank=True)
   custom = models.BooleanField(default=False)
   def __str__(self):
     return self.name

 class SecurityGroup(models.Model):
   name = models.CharField(max_length=20)
   description = models.CharField(max_length=30, null=True, blank=True)
   ins_date = models.DateField(default=timezone.now)
   mod_date = models.DateField(default=timezone.now)
   def __str__(self):
     return self.name

 class NACL(models.Model):
   machine = models.ForeignKey(Machine)
   securityGroup = models.ForeignKey(SecurityGroup)
   ins_date = models.DateField(default=timezone.now)
   mod_date = models.DateField(default=timezone.now)

 BYADMIN_CHOICES = ((1, "Input"),(0, "Output"),)

 class Rule(models.Model):
   type_rule = models.CharField(max_length=10)
   description = models.CharField(max_length=30, null=True, blank=True,   default=None)
   protocol = models.CharField(max_length=10)
   port_range_min = models.IntegerField()
   port_range_max = models.IntegerField(null=True, blank=True)
   #sg_object = models.CharField(max_length=20, null=True, blank=True)
   ip_object = models.GenericIPAddressField(null=True, blank=True)
   securityGroup_object = models.ForeignKey(SecurityGroup, null=True, blank=True)
   bound = models.BooleanField(choices=BYADMIN_CHOICES, default=1) #if           bound == True: input else: output 
  class Meta:
    unique_together = (("type_rule", "protocol", "port_range_min", "bound"))
  def __str__(self):
    return ("INPUT: " if(self.bound) else "OUTPUT: ") + self.type_rule + " " + self.protocol + " " + str(self.port_range_min) + " " + (str(self.securityGroup_object) if(self.securityGroup_object != None) else self.ip_object)

 class Sg_rule(models.Model):
   securityGroup = models.ForeignKey(SecurityGroup)
   rule = models.ForeignKey(Rule)
   ins_date = models.DateField(default=timezone.now)
   mod_date = models.DateField(default=timezone.now)

 class Service(models.Model):
   name = models.CharField(max_length=20)
   macroService = models.CharField(max_length=20)
   def __str__(self):
     return self.name


 class M_Service(models.Model):
   machine = models.ForeignKey(Machine)
   service = models.ForeignKey(Service)
   ins_date = models.DateField(default=timezone.now)
   mod_date = models.DateField(default=timezone.now)

失败的迁移:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models
import datetime
from django.utils.timezone import utc


class Migration(migrations.Migration):

dependencies = [
    ('adminApp', '0009_auto_20160229_1716'),
]

operations = [
    migrations.AddField(
        model_name='rule',
        name='ip_object',
        field=models.GenericIPAddressField(default=datetime.datetime(2016, 2, 29, 17, 17, 42, 169176, tzinfo=utc)),
        preserve_default=False,
    ),
    migrations.AlterField(
        model_name='rule',
        name='sg_object',
        field=models.CharField(max_length=20),
    ),
]

1 个答案:

答案 0 :(得分:0)

您的迁移尝试使用日期时间作为IP地址字段的默认值。这没有意义!

field=models.GenericIPAddressField(default=datetime.datetime(2016, 2, 29, 17, 17, 42, 169176, tzinfo=utc)),