【问题标题】:Why is Django shell running a function using not the inputted parameters?为什么 Django shell 不使用输入的参数运行函数?
【发布时间】: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


    【解决方案1】:

    我可能是错的,这个问题有点含糊,你从来没有对问题给出确切的描述,但在我看来这是一个 python 问题而不是 django 问题。 timess 类有一个名为 lists 的类属性,您将其视为实例属性。 Here's a concise description of the difference in a similar case。对于timess 实例,self.list 是对类字典的本地引用。调用timess(...) 会更改类字典中的值,因此它会针对所有实例进行更改。我不会评论其余的代码,但要让这个类按(我认为)你期望的那样工作,试试这个:

    class timess():
    
        def __init__(self, nonay, uplay, servertype):
            self.lists = {
                "none" : [nonay],
                "uploaded" : [uplay],
            }
            self.servername = servertype
    

    【讨论】:

      猜你喜欢
      • 2013-10-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-17
      • 1970-01-01
      • 2019-06-25
      • 2022-01-23
      • 2014-12-18
      • 1970-01-01
      相关资源
      最近更新 更多