我收到一个错误,AttributeError:“QuerySet”对象没有属性“area”。 我想解析excel并把它放到模型中(城市和地区&用户)。 我写的
fourrows_transpose = list(map(list, zip(*fourrows)))
val3 = sheet3.cell_value(rowx=0, colx=9)
user3 = Companyransaction.objects.filter(corporation_id=val3)
print(user3)
if user3:
area = Area.objects.filter(name="America")
pref = Prefecture.objects.create(name="prefecture", area=user3.area)
city = City.objects.create(name="city", prefecture=pref)
price_u1000 = Price.upper1000.objects.get(city=city)
price_500_1000 = Price.from500to1000.objects.get(city=city)
price_u500 = Price.under500.objects.get(city=city)
pref.name = "NY"
pref.save()
for i in range(len(fourrows_transpose)):
city.name = fourrows_transpose[i][1]
city.save()
print(fourrows_transpose[i][1])
price_u1000.name = fourrows_transpose[i][2]
price_u1000.save()
print(fourrows_transpose[i][2])
price_500_1000.name = fourrows_transpose[i][3]
price_500_1000.save()
print(fourrows_transpose[i][3])
price_u500.name = fourrows_transpose[i][4]
price_u500.save()
print(fourrows_transpose[i][4])
在模型.py是
^{pr2}$我想把这些数据
[['America', '', '', '', ''], ['', '', 'u1000', '500~1000', 'd500'], ['NY', 'City A', '×', '×', '×'], ['', 'City B', '×', '×', '×'], ['', 'City C', '×', '×', '×'], ['', 'City D', '×', '×', '×'], ['', 'City E', '×', '×', '×']]
像“美国”到地区,城市A到城市名称和价格。我该怎么解决这个问题?我应该写什么?在
正如错误所说,
user3
是一个查询集,而不是一个模型实例。在filter
始终返回查询集,即使只有一个匹配项。如果需要实例,应使用.get
:您看到此错误是因为您试图访问查询集上的
.area
,而不是访问单个Companyransaction
实例。当您执行.filter
操作时,将返回一个queryset。如果您确定只返回一个对象,我建议您更改:为此:
^{pr2}$相关问题 更多 >
编程相关推荐