2016-04-08 25 views
0

'id' hatasına sahip değil. Django üzerinde çalışıyorum ve bir sorunla karşılaştım. Kimin damgası product_sync_ts büyükse tablo üründen ürünün tüm kimlikleri almaya çalışıyorum'long' nesnesi django

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 

: Bu views.py için kod parçası olduğunu. Tablo ProductUPC tablosu için de yaptığım şey.

productUPC modeli

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) 

Ürün modeli

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) 

alıyorum hatadır:

'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' 

traceback:

product_sync_ts bir dizidir ama saha update_ts bir DateTimeField çünkü
/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) 

cevap

0

Sen update_ts__gt=product_sync_ts yapamaz. Önce product_sync_ts değişkenini DateTimeField'a dönüştürmeniz ve ardından sorguyu yapmanız gerekir.

+0

Sadece update_ts dosyasını string'e dönüştüremiyorum ??? –

+0

Sorunu anlamıyorum. "Update_ts" değerini string'e dönüştürebilirsiniz, ancak django ORM sorgusunu yapmak için dize formatını kullanamazsınız. –

+0

Evet, bu kadar saçma bir soru için üzgünüm;) –