'长'对象没有属性' id' django中的错误

时间:2016-04-08 17:46:57

标签: python django django-models

我正在研究django并陷入困境。这是 views.py

的代码段
    tz = pytz.timezone('Asia/Kolkata')
    product_sync_ts= datetime.now(tz)
    product_sync_ts = "2016-03-10 14:41:48.901013+05:30"
    product = Product.objects.filter(update_ts__lt=product_sync_ts , enabled_flag = True , ).values_list('id', flat=True)
    product_upcs = ProductUPC.objects.filter(update_ts__lt = product_sync_ts , enabled_flag=True).values_list('product' , flat=True)

    print product
    print product_upcs

我正在尝试从时间戳大于 product_sync_ts 的表格产品中获取所有产品ID。我也在为表ProductUPC表做同样的事情。

productUPC模型

class ProductUPC(models.Model):
    product = models.ForeignKey('Product', related_name='upcs')
    upc = models.CharField(max_length=20, unique=True, null=False)
    install_ts = models.DateTimeField(auto_now_add=True)
    update_ts = models.DateTimeField(auto_now=True)
    enabled_flag = models.BooleanField(default=True)

产品型号

 class Product(models.Model):
    name = models.TextField()
    price = models.IntegerField()
    enabled_flag = models.BooleanField(default=True)
    install_ts = models.DateTimeField(auto_now_add=True)
    update_ts = models.DateTimeField(auto_now=True)

我得到的错误是:

'long' object has no attribute 'id'
Request Method: GET
Request URL:    http://127.0.0.1:8000/products/
Django Version: 1.8
Exception Type: AttributeError
Exception Value:    
'long' object has no attribute 'id'

回溯:

/Users/folder/venv/lib/python2.7/site-packages/django/core/handlers/base.py in get_response
                    response = middleware_method(request, callback, callback_args, callback_kwargs)
                    if response:
                        break
            if response is None:
                wrapped_callback = self.make_view_atomic(callback)
                try:
                                response = wrapped_callback(request, *callback_args, **callback_kwargs) ...
                except Exception as e:
                    # If the view raised an exception, run it through exception
                    # middleware, and if the exception middleware returns a
                    # response, use that. Otherwise, reraise the exception.
                    for middleware_method in self._exception_middleware:
                        response = middleware_method(request, e)

1 个答案:

答案 0 :(得分:0)

您不能update_ts__gt=product_sync_ts,因为product_sync_ts是字符串,但字段update_tsDateTimeField。您需要先将变量product_sync_ts转换为DateTimeField然后再进行查询。