Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何放置每个模型城市&;县及;地区及;用户?_Python_Django - Fatal编程技术网

Python 如何放置每个模型城市&;县及;地区及;用户?

Python 如何放置每个模型城市&;县及;地区及;用户?,python,django,Python,Django,我想把列表的数据放到市、州、地区和用户模型中? 我写 models.py是 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

我想把列表的数据放到市、州、地区和用户模型中? 我写

models.py是

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)
在打印(四行转置)中,显示

[['America', '', '', '', ''], ['', '', 'u1000', '500~1000', 'd500'], ['NY', 'City A', '×', '×', '×'], ['', 'City B', '×', '×', '×'], ['', 'City C', '×', '×', '×'], ['', 'City D', '×', '×', '×'], ['', 'City E', '×', '×', '×']]
我想把“美国”放在州的
地区
,把城市A放在城市的
名称
和× 以普莱斯的名义。
所以,我想把这些数据放到tp user3.update(XXXXX)中。我应该向XXX写什么?我该怎么做?

如果您有现有的地区、城市和价格实例,那么您必须分别调用每个实例并进行更新

示例(假设用户和地区通过区域属性链接)

pref = Prefecture.objects.get(area=user3.area)
city = City.objects.get(prefecture=pref)
price = Price.objects.get(city=city)

pref.name = "whatever you want"
pref.save()

city.name = "whatever you want"
city.save()

price.name = "whatever you want"
price.save()
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)
如果您没有上述模型的现有实例,则必须为每个模型创建一个实例,并将它们相互链接

示例(假设用户和地区通过区域属性链接)

pref = Prefecture.objects.get(area=user3.area)
city = City.objects.get(prefecture=pref)
price = Price.objects.get(city=city)

pref.name = "whatever you want"
pref.save()

city.name = "whatever you want"
city.save()

price.name = "whatever you want"
price.save()
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)

希望这有帮助

您是否有地区、城市和价格的现有实例?或者您想创建新的并将其链接到此用户?@N.Ivanov我想创建新的并将其链接到此用户。请检查我的答案:)