简体   繁体   中英

Update postgresql Database Column in Django

I'm trying to save data to a 2D array within a PostgreSql database, the data is stored as following:

"[('User 1', 'User 2', 'User3', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '')]"

This represents a single array within a single column of the database, this data is accessed via the following Python Code, which requests the correct row in the database and then the relevant column

q = DBReports.objects.all().filter(name__contains = username)  
q = q.values_list(columnName)

In order to iterate through the data retrieved, it is then placed into a list

 users_list = ast.literal_eval(q[0][0])

The correct array within the column is then selected using the following code:

 for list in users_list:
                if user in list[2]:
                    listToUse = list

The issue i'm running into is that now with the retrieved data, I need to change it and resave to the database (listToUse is changed with the blanks filled in), such that the array may now for example represent

"[('User 1', 'User 2', 'User3', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '19')]"

How would i go about updating the data that was stored in the array, or replacing the array with the new created list of the same structure, I'm using django's database abstraction to handle all the data

This was solved by taking all information from the returned queryset, storing this in variables/list, updating those variables with the required information, and then saving this to the database.

Requesting information:

q = DBReports.objects.all().filter(name__contains = assessor)  
qValues = q.values_list(columnName)

if len(qValues) > 0:
            users_list = ast.literal_eval(qValues[0][0])

            usernameToAppend = q[0].username
            nameToAppend = q[0].name
            mn= users_list[0][1]
            EOS= q[0].EOS
            RW= q[0].RW
            temp = (assessor, mn, assessee, '','','','','','','','','','','','','','','','','','')
            users_list.append(temp)
            recordToAppend = DBReports(username = usernameToAppend,name = nameToAppend, RW = users_list, EOS = evaluationsOfSelf, RW = RW)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM