0

I have a function that is supposed to update Django objects, which each have two date fields, named "production" and "development". The function reads from a python object, containing a list of Django objects along with their new dates, as well as a single label indicating for the entire list whether the fields to be updated are "production" or "development".

For testing, I have two python objects, containing two different lists of django objects and dates, one for "production" and one for "development". When I call the function on one of these objects, no matter what, it's the list and dates in the "production" object that get updated, though whether the info goes to the "development" or "production" fields in those django objects still depends on the label of the initial python object being called on. I have no idea why this is happening.

Python class and initialization of two python objects:

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")

Function definitions:

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 object model:

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 shell input:

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 Enterprise Version 1803

EmilSohlberg
  • 45
  • 1
  • 8

1 Answers1

0

I could be wrong, the question is a little vague and you never give an exact description of the problem, but it looks to me to be a python problem not a django one. The timess class has a class attribute called lists which you are treating as an instance attribute. Here's a concise description of the difference in a similar case. For a timess instance, self.list is a local reference to the class dictionary. Calling timess(...) changes the value in the class dictionary, so it's changed for all instances. I won't comment on the rest of the code, but to make this class work as (I think) you expect, try this instead:

class timess():

    def __init__(self, nonay, uplay, servertype):
        self.lists = {
            "none" : [nonay],
            "uploaded" : [uplay],
        }
        self.servername = servertype
RishiG
  • 2,790
  • 1
  • 14
  • 27