python - How can I put each model City&Prefecture&Area&User? -
i wanna put data of list city&prefecture&area&user model ? wrote
book3 = xlrd.open_workbook('./data/excel1.xlsx') sheet3 = book3.sheet_by_index(0) data_dict = ordereddict() tag_list = sheet3.row_values(0) tag_list1_user_id = tag_list[9] fourrows = [] row_index in range(7, sheet3.nrows): row = sheet3.row_values(row_index) if len(fourrows) == 5: fourrows=[] fourrows.append(row) fourrows_transpose = list(map(list, zip(*fourrows))) print(fourrows_transpose) val3 = sheet3.cell_value(rowx=0, colx=9) user3 = companyransaction.objects.filter(user_id=val3) if user3: user3.update(xxxxx)
models.py is
class area(models.model): name = models.charfield(max_length=20, verbose_name='エリア名', null=true) class user(models.model): user_id = models.charfield(max_length=200,null=true) area = models.foreignkey('area',null=true, blank=true) class prefecture(models.model): name = models.charfield(max_length=20, verbose_name='prefecture') area = models.foreignkey('area', null=true, blank=true) class city(models.model): name = models.charfield(max_length=20, verbose_name='city') prefecture = models.foreignkey('prefecture', null=true, blank=true) class price(models.model): name = models.charfield(max_length=20, verbose_name='price') price_range = ( ('a', 'u500'), ('b', '500-1000'), ('c', 'u1000'), ) price_range = models.charfield(max_length=1, choices=price_range) city = models.foreignkey('city', null=true, blank=true)
in print(fourrows_transpose),it shown
[['america', '', '', '', ''], ['', '', 'u1000', '500~1000', 'd500'], ['ny', 'city a', '×', '×', '×'], ['', 'city b', '×', '×', '×'], ['', 'city c', '×', '×', '×'], ['', 'city d', '×', '×', '×'], ['', 'city e', '×', '×', '×']]
i wanna put 'america' prefecture's area
, city city's name
, × to price's name . so,i wanna put these data tp user3.update(xxxxx).what should write xxx?how can it?
if have existing instances of prefecture, city, , price have call each 1 , update separately.
example (assuming user , prefecture linked area property):
pref = prefecture.objects.get(area=user3.area) city = city.objects.get(prefecture=pref) price = price.objects.get(city=city) pref.name = "whatever want" pref.save() city.name = "whatever want" city.save() price.name = "whatever want" price.save()
if not have existing instances of said above models, have create instance each one, , link them each other.
example (assuming user , prefecture linked area property):
pref = prefecture.objects.create(name="yourname", area=user3.area) city = city.objects.create(name="yourname", prefecture=pref) price = price.objects.create(name="yourname", city=city)
hope helps!
Comments
Post a Comment