[英]AttributeError: 'QuerySet' object has no attribute 'area'
I got an error,AttributeError: 'QuerySet' object has no attribute 'area' . 我收到一个错误,AttributeError:'QuerySet'对象没有属性'area'。 I wanna parse excel and put it to the model(City&Prefecture&Area&User) . 我想解析excel并将其放入模型(City&Prefecture&Area&User)。 I wrote 我写
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])
models.py is models.py是
class Area(models.Model):
name = models.CharField(max_length=20, verbose_name='area', 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')
city = models.ForeignKey('City', null=True, blank=True)
I wanna put these data 我想把这些数据
[['America', '', '', '', ''], ['', '', 'u1000', '500~1000', 'd500'], ['NY', 'City A', '×', '×', '×'], ['', 'City B', '×', '×', '×'], ['', 'City C', '×', '×', '×'], ['', 'City D', '×', '×', '×'], ['', 'City E', '×', '×', '×']]
to models which is like 'America' to Prefecture's area and City A to City's name and × to Price's name . 适用于县区为“ America”的模型,适用于City的城市为A,适用于Price的名称为×的模型。 How can I fix this?What should I write it? 我该如何解决?该怎么写?
As the error says, user3
is a QuerySet, not a model instance. 如错误所述, user3
是QuerySet,而不是模型实例。
filter
always returns a QuerySet, even if there is only a single match. filter
总是返回一个QuerySet,即使只有一个匹配项也是如此。 If you want an instance, you should use .get
: 如果需要实例,则应使用.get
:
user3 = Companyransaction.objects.get(corporation_id=val3)
You are seeing this error since you are attempting to access .area
on a queryset not on a single Companyransaction
instance. 由于尝试访问.area
单个Companyransaction
实例上的查询.area
上的.area
,因此您看到此错误。 When you do a .filter
a queryset is returned. 当您执行.filter
将返回一个.filter
。 If you are sure only a single object will be returned i would suggest you change: 如果您确定只返回一个对象,我建议您更改:
user3 = Companyransaction.objects.filter(corporation_id=val3)
to this: 对此:
user3 = Companyransaction.objects.get(corporation_id=val3)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.