Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 属性错误:';延迟属性';对象没有属性';对象';_Python_Django - Fatal编程技术网

Python 属性错误:';延迟属性';对象没有属性';对象';

Python 属性错误:';延迟属性';对象没有属性';对象';,python,django,Python,Django,我收到一个错误,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(nam

我收到一个错误,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\u u1000=price.upper700.objects.get(city=city)
错误。 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):
    upper1000 = models.CharField(max_length=20, verbose_name='u1000', null=True)
    from500to1000 = models.CharField(max_length=20, verbose_name='500~1000', null=True)
    under500 = models.CharField(max_length=20, verbose_name='d500', null=True)
    city = models.ForeignKey('City', null=True, blank=True)

我应该怎么做才能解决这个问题?我应该如何编写它?

这是因为upper1000或其他任何字段都没有属性对象。对象存在于模型类中

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

price_u1000 = Price.objects.filter(city=city, upper1000__isnull=False, from500to1000__isnull=True, under500__isnull=True)
我建议创建一个选择字段,而不是3个看起来相互排斥的不同字段

class Price(models.Model):

    UNDER_500 = 'under 500'
    FROM_500_TO_1000 = 'from 500 to 1000'
    UPPER_1000 = 'upper 1000'

    PRICE_CHOICES = [
        (UNDER_500, pgettext_lazy('Price under 500')),
        (FROM_500_TO_1000, pgettext_lazy('Price from 500 to 1000')),
        (UPPER_1000, pgettext_lazy('Price above 1000'))
    ]

    price_range = models.CharField(
        verbose_name=ugettext_lazy('Price range'),
        max_length=25,
        choices=PRICE_CHOICES,
        default=UNDER_500
    )

    city = models.ForeignKey('City', null=True, blank=True)

    def clean(self):
        super(Price, self).clean()
        # implement extra constraints here.
一些阅读资料:

thx ur注释。我不想排除从500到1000以及500以下(如果大于1000)。我想将这些模型放入数据。好的,但第一句解释了您的问题,第一个查询是您应该如何从模型中查询数据。请记住在查询此类数据时使用过滤器,因为除了id/pk之外,您的任何字段上都没有unique=True。因此,您必须期望从查询返回多行。