2017-09-12 10 views
0

Ich habe einen Fehler, AttributeError: 'QuerySet' Objekt hat kein Attribut 'Bereich'. Ich möchte analysieren Excel und legte es auf das Modell (Stadt & Präfektur & Bereich & Benutzer). Ich schriebAttributeError: 'QuerySet' Objekt hat kein Attribut 'Bereich'

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 ist

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) 

ich diese Daten stellen wanna

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

auf Modelle, die wie 'America' zu Präfektur der Region und Stadt A Stadt Name ist und × zum Preisnamen. Wie kann ich das beheben? Was soll ich schreiben?

Antwort

0

Sie diesen Fehler sehen, da Sie versuchen, .area auf einem queryset zuzugreifen nicht auf einer einzigen Companyransaction Instanz. Wenn Sie eine .filter tun, wird ein Abfrage-Set zurückgegeben. Wenn Sie sicher sind, nur ein einzelnes Objekt wird zurückgegeben, ich würde vorschlagen, Sie ändern:

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

dazu:

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

Wie der Fehler sagt, ist user3 ein QuerySet, keine Modellinstanz.

filter gibt immer ein QuerySet zurück, auch wenn nur eine einzige Übereinstimmung vorhanden ist. Wenn Sie eine Instanz wollen, sollten Sie .get verwenden:

user3 = Companyransaction.objects.get(corporation_id=val3) 
Verwandte Themen