SoFunction
Updated on 2024-11-19

Example of django model updating data via dictionary

example:

Tables in models

# models
class UserInfo():
  id = (primary_key=True)
  user_name = (max_length=100, verbose_name='Username')
  cellphone = (max_length=100, blank=False, null=True, verbose_name='Cell phone')
  password = (max_length=225, verbose_name='Password')

Update the code as follows:

datas = {'user_name': 'Zhang San'}
user = (pk=1)
(**datas)

Look at the userinfo table data in the database again, and find that the data has been modified

Additional knowledge:How to deposit a dictionary into mysql in Django in Python

I'll cut to the chase, or just look at the code!

import json
from test_case.models import Modules as m

data = {'a': 1, "b": 2}

# Turn dictionaries into strings
datastr = (data)
print(type(datastr), datastr)

# Into the database
(name=datastr, item_id='10')

# Fetch data from the database
data1 = (name=datastr).name

# Turn data into a dictionary
datadact = (data1)
print(type(datadact), datadact)

# Results

<class 'str'> {"a": 1, "b": 2}
<class 'dict'> {'a': 1, 'b': 2}

The above example of this django model updating data via dictionary is all I have to share with you, I hope it can give you a reference, and I hope you will support me more.