AttributeError:“DeferredAttribute”对象没有属性“objects”

2024-09-30 16:25:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我收到一个错误,AttributeError:“DeferredAttribute”对象没有属性“objects”。 我想解析excel并把它放到模型中(城市和地区&用户)。我写的

user3 = User.objects.filter(corporation_id=val3).first()
if user3:
   area = Area.objects.filter(name="America").first()
   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(2,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])

回溯表明此代码price_u1000 = Price.upper700.objects.get(city=city)是错误的。 模型.py是

^{pr2}$

我该怎么解决这个问题?我该怎么写?在


Tags: namecitygetobjectssaveareapriceprint
1条回答
网友
1楼 · 发布于 2024-09-30 16:25:53

这是因为upper1000或what ever字段没有属性对象。对象存在于模型类中。在

如果1000以上,500到1000和500以下是互斥的,你可以这样做。但请记住,如果是这种情况,您应该在models clean函数中处理互斥性。在

price_u1000 = Price.objects.filter(city=city, upper1000__isnull=False, from500to1000__isnull=True, under500__isnull=True)

我建议用3个不同的字段,而不是相互排斥

^{2}$

一些读数: https://docs.djangoproject.com/en/1.11/ref/models/querysets/

相关问题 更多 >