2017-09-12 19 views
0

AttributeError: 'QuerySet'オブジェクトに 'area'属性がありません。 私は構文解析してモデルに入れたいと思っています(都市&県&エリア&ユーザー)。 は、私は市の名前に県の面積に「アメリカのシティAのようなものであるのモデルにこれらのデータAttributeError: 'QuerySet'オブジェクトに 'area'属性がありません

[['America', '', '', '', ''], ['', '', 'u1000', '500~1000', 'd500'], ['NY', 'City A', '×', '×', '×'], ['', 'City B', '×', '×', '×'], ['', 'City C', '×', '×', '×'], ['', 'City D', '×', '×', '×'], ['', 'City E', '×', '×', '×']] 

を入れたく

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) 

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は書きました価格の名前に×を付けます。これを修正するにはどうしたらいいですか?

答えて

0

.filterを実行すると、クエリセットが返されます。あなたは、単一のオブジェクトが返されますが確信している場合、私はあなたが変更を示唆している:これまで

user3 = Companyransaction.objects.filter(corporation_id=val3) 

user3 = Companyransaction.objects.get(corporation_id=val3) 
0

エラーとして、user3はモデルインスタンスではなく、QuerySetです。

filterは、一致が1つしかない場合でも常にQuerySetを返します。あなたは、インスタンスをしたい場合は、.getを使用する必要があります:あなたがいない単一Companyransactionインスタンス上でクエリセットに.areaにアクセスしようとしているので、あなたは、このエラーが表示されている

user3 = Companyransaction.objects.get(corporation_id=val3) 
関連する問題