python - Django `loaddata` pretty way to load same objects with different pk (id) -


i use loaddata load fixtures (django doc.)

for example have example.json:

[  {    "model": "foo.bar",    "pk": 1,    "fields": {      "name": "dmytryi"    }  },   {    "model": "foo.bar",    "pk": 2,    "fields": {      "name": "dmytryi"    }  },  ... (repeat until pk not 10 increasing pk one)  {    "model": "foo.bar",    "pk": 10,    "fields": {      "name": "dmytryi"    }  }, ] 

how see, have 10 same objects (same name same model), different pk value. after loaddata see in database 10 objects. ok.

give hint how reduce code, leave same result, if exists. you!

base =  {    "model": "foo.bar",    "fields": {      "name": "dmytryi"    } }  data = [{**base, **{'pk': id}} id in range(1, 11)] 

this unpack base dictionary , {'pk': id} dictionary new dictionary. output looks this:

[{'fields': {'name': 'dmytryi'}, 'model': 'foo.bar', 'pk': 0},  {'fields': {'name': 'dmytryi'}, 'model': 'foo.bar', 'pk': 1},  {'fields': {'name': 'dmytryi'}, 'model': 'foo.bar', 'pk': 2},  {'fields': {'name': 'dmytryi'}, 'model': 'foo.bar', 'pk': 3},  {'fields': {'name': 'dmytryi'}, 'model': 'foo.bar', 'pk': 4},  {'fields': {'name': 'dmytryi'}, 'model': 'foo.bar', 'pk': 5},  {'fields': {'name': 'dmytryi'}, 'model': 'foo.bar', 'pk': 6},  {'fields': {'name': 'dmytryi'}, 'model': 'foo.bar', 'pk': 7},  {'fields': {'name': 'dmytryi'}, 'model': 'foo.bar', 'pk': 8},  {'fields': {'name': 'dmytryi'}, 'model': 'foo.bar', 'pk': 9}] 

you make shorter this:

[{**{'fields': {'name': 'dmytryi'}, 'model': 'foo.bar',}, **{'pk': i}} in range(1, 11)] 

but think that's worse.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -