2016-04-08 66 views
0

我在Django上工作並陷入一個問題。這是views.py代碼片段:'long'對象在django中沒有屬性'id'錯誤

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 

我試圖讓產品的所有的ID從表產品,其時間戳比product_sync_ts更大。我也爲表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) 

回答

0

你不能這樣做update_ts__gt=product_sync_ts,因爲product_sync_ts是一個字符串,但是場update_tsDateTimeField。您需要首先將變量product_sync_ts轉換爲DateTimeField,然後執行查詢。

+0

不能我只是將update_ts轉換爲字符串? –

+0

我不明白你的問題。您可以將各個'update_ts'值轉換爲字符串,但不能使用字符串格式來執行django ORM查詢。 –

+0

是的,我得到了那個...對不起,這樣一個愚蠢的問題;) –

相關問題