writing to model field in django / python
i'm trying to populate custom user profile fields with data from social networks. i created profile fields like:
currentjobtitle1
currentjobtitle2
currentjobtitle3
pastjobtitle1
pastjobtitle2
pastjobtitle3
I'm a newb in every way, so i tried to code this more efficiently than normal and have become very confused. I'm sure my code is wrong in more ways than one, but if i can just get it to work:
pnum=0
cnum=0
for x in clean_work_obj[:]:
if x['end_date']:
var='past'
count=pnum+1
else:
var='current'
count=cnum+1
if count>3:
break
else:
SarProfile.var+'jobtitle'+str(count)= x['position']['name']
clean_work_obj is the dict i received thru the social network api.
I had ex开发者_JS百科pected it to write out: SarProfile.pastjobtitle1 or SarProfile.currentjobtitle3, for example, and populate that field with the "position / name" data. it breaks because there is no field called "var" in SarProfile.
setattr(SarProfile, '%sjobtitle%d' % (var, count), x['position']['name'])
But you probably want to create an instance of SarProfile
first, and use that instead.
精彩评论