我已经开发了一个用于更新数据库的代码,但是现在我想编码,如果值是更改数据库插入数据而不是更新数据库。这是我的代码: -
def update_Sprint(self, ps):
id = ps.projectid
data=requests.get('https://example.com/api/v1/xyz/'+str(ps.projectid)+'/Iterations?include=[Id,Name,StartDate,EndDate,UserStories-Effort-Sum,UserStories-EffortCompleted-Sum]&token='+settings.AUTH_TOKEN+'&format=json')
data=json.loads(data.text)
data=data['Items']
if len(data)>0:
for data_ in data:
sprint_id=str(data_['Id']
sps = Day.objects.filter(sprintid=sprint_id, project__projectid=ps.projectid)
print ' creating new sprint '+str(sprint_id)
sp = Day(sprintid=sprint_id, project=ps)
sp = update_model(sp, sprint_name = Name,sprint_end_date = EndDate, sprint_start_date = StartDate, usesteff_sum = Effor_Sum,usesteffc_sum = Effort_Com,)
#self.update_story(sp)
#self.update_Bug(sp)
def update_model(obj,** params): “”“
:param obj: generic model object
:param params: fields, value dict of the obj's model
:return: updated object
"""
is_updating = False
for key in params:
try:
field = getattr(obj, key)
value = params[key]
if field==value:
print 'same value'
elif ((not value) and (not field)): print 'No value for '+key
else:
print 'updating value'
is_updating = True
setattr(obj, key, value)
# obj.field = value
except AttributeError as ex:
print ex
if is_updating:
obj.save()
return obj
如果字段和值不相同,如何将数据插入数据库。我不想更新表。