【发布时间】:2018-08-07 17:21:30
【问题描述】:
我有一个应该更新 Django 对象的函数,每个对象都有两个日期字段,名为“生产”和“开发”。该函数从一个 python 对象中读取,其中包含一个 Django 对象列表及其新日期,以及一个标签,指示整个列表要更新的字段是“生产”还是“开发”。
为了测试,我有两个 python 对象,包含两个不同的 django 对象和日期列表,一个用于“生产”,一个用于“开发”。当我在这些对象之一上调用该函数时,无论如何,更新的是“生产”对象中的列表和日期,尽管信息是否进入这些 django 对象中的“开发”或“生产”字段仍然取决于被调用的初始 python 对象的标签。我不知道为什么会这样。
Python类和两个python对象的初始化:
import pytz
import datetime
class timess():
#Reflects update statuses for the two servers
lists = {
"none" : [],
"uploaded" : [],
}
servername = ""
def __init__(self, nonay, uplay, servertype):
self.lists["none"] = nonay
self.lists["uploaded"] = uplay
self.servername = servertype
timezone = pytz.timezone("America/Chicago")
d1 = datetime.datetime(1999, 8, 12, 12, 32, 41)
d2 = datetime.datetime(1996, 8, 12, 12, 32, 41)
d3 = datetime.datetime(2004, 8, 12, 12, 32, 41)
d1a = timezone.localize(d1)
d2a = timezone.localize(d2)
d3a = timezone.localize(d3)
p1 = datetime.datetime(1997, 8, 12, 12, 32, 41)
p2 = datetime.datetime(2002, 8, 12, 12, 32, 41)
p3 = datetime.datetime(2006, 8, 12, 12, 32, 41)
p4 = datetime.datetime(1992, 8, 12, 12, 32, 41)
p1a = timezone.localize(p1)
p2a = timezone.localize(p2)
p3a = timezone.localize(p3)
p4a = timezone.localize(p4)
dnonay = [
("mvol/0004/1905/0404", None),
("mvol/0004/1920/1111", None),
("mvol/0004/1930/0812", None),
("mvol/0004/1905/0214", None),
("mvol/0004/1930/1001", None),
("mvol/0004/1905/0917", None),
("mvol/0004/1930/0712", None),
("mvol/0004/1920/1202", None),
]
duplay = [
("mvol/0004/1905/0130", d1a),
("mvol/0004/1920/0624", d2a),
("mvol/0004/1930/0311", d3a),
]
dtimess = timess(dnonay, duplay, "development")
pnonay = [
("mvol/0004/1905/0130", None),
("mvol/0004/1920/1111", None),
("mvol/0004/1905/0214", None),
("mvol/0004/1930/0311", None),
("mvol/0004/1905/0917", None),
("mvol/0004/1930/0712", None),
("mvol/0004/1920/0624", None)
]
puplay = [
("mvol/0004/1905/0404", p1a),
("mvol/0004/1920/1202", p2a),
("mvol/0004/1930/0812", p3a),
("mvol/0004/1930/1001", p4a),
]
ptimess = timess(pnonay, puplay, "production")
函数定义:
from listpage.timess import *
from listpage.models import *
def integratetimess(timess):
'''
Takes information in server update list and applies it
to mvolFolder objects in site. Does not work properly.
'''
if timess.servername == "development":
for line in timess.lists["uploaded"]:
target = mvolFolder.objects.get(name = line[0])
target.dev = line[1]
target.save()
if timess.servername == "production":
for line in timess.lists["uploaded"]:
target = mvolFolder.objects.get(name = line[0])
target.pro = line[1]
target.save()
def integrateot(namey, date, servertype):
'''
This function works when called on
a django mvolFolder object outside of the
timess object
'''
target = mvolFolder.objects.get(name = namey)
if(servertype == "development"):
print(namey)
print("development, initially:")
print(target.dev)
print(target.pro)
target.dev = date
print("development, after changing")
print(target.dev)
print(target.pro)
target.save()
if(servertype == "production"):
print(namey)
print("production, initially:")
print(target.dev)
print(target.pro)
target.pro = date
print("production, after changing")
print(target.dev)
print(target.pro)
target.save()
def integratetimesx(timess):
'''
However, when integrateot is called to loop on the
list in the python object, there's the same issue
as the first function
'''
for line in timess.lists["uploaded"]:
integrateot(line[0], line[1], timess.servername)
Django 对象模型:
from django.db import models
# Create your models here.
class Folder(models.Model):
name = models.CharField(max_length = 19)
def __str__(self):
return self.name
class mvolFolder(Folder):
date = models.DateTimeField(null = True)
valid = models.NullBooleanField(null = True)
dev = models.DateTimeField(null = True)
pro = models.DateTimeField(null = True)
parent = models.ForeignKey('self', on_delete=models.CASCADE, null = True, related_name = 'children')
Django 外壳输入:
from listpage.models import mvolFolder
from listpage.timess import *
from listpage.convtimess import *
integratetimesx(ptimess)
integratetimesx(dtimess)
Python 3.7.0
Django 2.0.8
Windows 10 企业版 1803
【问题讨论】:
标签: python django function django-models