简体   繁体   中英

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 expected 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.

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