Save query in Django -
i have model pptlcode
class pptlcode(models.model): # many 1 relationship pptlconfig code = models.charfield(max_length = 255, unique = true) pconf = models.foreignkey('pptlconfig', related_name= 'codes')
i've list of codes. want save each code in list reference 1 pconf
. can utilize iteration doing this. curious know, whether there's 1 line solution it?
currently i'm doing this:-
for code in code_list: obj = pptlcode(code=code, pconf=pconf_obj) obj.save()
use bulk create to save new objects in 1 query:
obj_list = [] code in code_list: obj_list.append(pptlcode(code = code, pconf = pconf_obj)) pptlcode.objects.bulk_create(obj_list)
or 1 line solution:
pptlcode.objects.bulk_create([pptlcode(code=code, pconf=pconf_obj) code in code_list])
note: bulk_create
avaliable in django >= 1.4
django django-queryset
No comments:
Post a Comment