Django模型中缺少字段?

时间:2015-12-02 22:33:13

标签: python django django-models

这是我的模特:

class SystemConfiguration(models.Model):
  idSystemConfiguration = models.AutoField(primary_key=True)
  mail_movement_subject = models.CharField(max_length=50, default="Movement Triggered")
  mail_snapped_subject  = models.CharField(max_length=50, default="Image Snapped")
  mail_carbon_subject   = models.CharField(max_length=50, default="Carbon Monoxide Triggered")
  mail_door_subject     = models.CharField(max_length=50, default="Door Activity Triggered")
  door1_name            = models.CharField(max_length=20, default="Garage Door")
  door2_name            = models.CharField(max_length=20, default="Garage Door 2")
  door3_name            = models.CharField(max_length=20, default="Garage Door 3")
  metric                = models.BooleanField()
  mail_temp_subject     = models.CharField(max_length=50, default="Temperature Triggered")
  min_temp              = models.DecimalField(decimal_places=1,max_digits=4)
  max_temp              = models.DecimalField(decimal_places=1,max_digits=4)
  mail_humidity_subject = models.CharField(max_length=50, default="Humidity Triggered")
  mail_to_user          = models.CharField(max_length=50, default="")
  min_humidity          = models.DecimalField(decimal_places=1,max_digits=3)
  max_humidity          = models.DecimalField(decimal_places=1,max_digits=3)

  def setDefaults(self):
    print "Creating SystemConfig..."
    self.mail_movement_subject = "Movement Triggered"
    self.mail_snapped_subject  = "Image Snapped"
    self.mail_carbon_subject   = "Carbon Monoxide Triggered"
    self.mail_door_subject     = "Door Activity Triggered"
    self.door1_name            = "Garage Door"
    self.door2_name            = "Garage Door 2"
    self.door3_name            = "Garage Door 3"
    self.metric                = False
    self.mail_temp_subject     = "Temperature Triggered"
    self.min_temp              = 28
    self.max_temp              = 100
    self.mail_humidity_subject = "Humidity Triggered"
    self.min_humidity          = 40
    self.max_humidity          = 50
    self.mail_to_user          = ""
    self.save()

  def __str__(self):
    return "SystemConfiguration:" + str(self.idSystemConfiguration) \
      + ",mail_movement_subject:" + str(self.mail_movement_subject) \
      + ",mail_snapped_subject:" + str(self.mail_snapped_subject) \
      + ",mail_carbon_subject:" + str(self.mail_carbon_subject) \
      + ",mail_door_subject:" + str(self.mail_door_subject) \
      + ",Door1Name:" + str(self.door1_name) \
      + ",Door2Name:" + str(self.door2_name) \
      + ",Door3Name:" + str(self.door3_name) \
      + ",Metric:" + str(self.metric) \
      + ",mail_temp_subject:" + str(self.mail_temp_subject) \
      + ",minTemp:" + str(self.min_temp) \
      + ",maxTemp:" + str(self.max_temp) \
      + ",mail_humidity_subject:" + str(self.mail_humidity_subject) \
      + ",minHumid:" + str(self.min_humidity) \
      + ",maxHumid:" + str(self.max_humidity) \
      + ",mail_to_user:" + str(self.mail_to_user) \
      + ",wan_port:" + str(self.wan_port) \
      + ",wan_video_port:" + str(self.wan_video_port) \
      + "\n"

这是我试图访问它的地方(注意我在django之外运行一个脚本,因此sys.path.apend(...)和os.environ [...] shenanigans:

import sys
import os
sys.path.append(os.path.abspath("/home/pi/garageMonitor/django/garageMonitor"))
os.environ['DJANGO_SETTINGS_MODULE'] = 'garageMonitor.settings'
from garageMonitor.models import SystemConfiguration

def handleMailFile(file):
    config = SystemConfiguration.objects.filter(idSystemConfiguration=1)
    config = config[0]
    for x in config.__dict__:
      print x
    print len(config.__dict__)

以下是运行的结果:

mail_snapped_subject
mail_humidity_subject
mail_carbon_subject
max_humidity 
door2_name 
max_temp
_state 
idSystemConfiguration
min_humidity 
mail_door_subject 
min_temp 
door3_name 
mail_temp_subject 
mail_movement_subject 
metric 
door1_name 
16

请注意,所有字段除了 mail_to_user都会在for循环中打印出来!我检查了数据库,果然,mail_to_user字段的长度和类型正确。

任何建议都会受到最高的赞赏。

我尝试添加另一个与实际工作相同的字段(更改名称。)Django在数据库中创建了该字段,但它没有显示在 dict 元素列表中? !?

我使用django代码在数据库中添加了另一列并重新运行了安装程序。编码添加了列(使用默认值),但仍然运行以下内容:

import sys
import os

sys.path.append(os.path.abspath("/home/pi/garageMonitor/django/garageMonitor"))
os.environ['DJANGO_SETTINGS_MODULE'] = 'garageMonitor.settings'
from garageMonitor.models import SystemConfiguration
config = SystemConfiguration.objects.filter(idSystemConfiguration=1)
config = config[0]
for x in config.__dict__:
  print x

print len(config.__dict__)

我只在 dict 对象中获得16个条目,并且新列不存在。我删除了所有.pyc文件,认为可能存在一些工件,但这也无济于事。

所以,我尝试了另一种方法。我通过复制原始模型并将其添加到models.py文件中创建了一个新模型(Jordan)。我运行了设置代码。它在数据库中正确创建了表,但是当我尝试访问模型时,出现以下错误:

ImportError: cannot import name Jordan

Arrrgggghhhhh !!!!!

尝试移动到另一个目录(django)并运行以下命令:

python...
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'garageMonitor.settings'
from garageMonitor.models import Jordan
config = Jordan.objects.filter(idSystemConfiguration=1)
config = config[0]
for x in config.__dict__:
  print x

这是我的输出:

_known_related_objects
_sticky_filter
_db
_result_cache
_prefetch_done
_for_write
query
model
_prefetch_related_lookups
_hints

(不像数据库表那样远程!)

0 个答案:

没有答案