2013-08-18 67 views
0

外鍵的字段我有如下:設置日期字段參考Django的

class FacturasMonthArchiveView(MonthArchiveView): 
    queryset = Factura.objects.all() 
    date_field = "pedido__fecha_pedido" 
    make_object_list = True 
    allow_future = True 
    template_name = 'ventas/facturas.html' 
    context_object_name = 'facturas_list' 

我有表Facturapedido,隨着許多信息,該領域的一個訂單引用是fecha_pedido,我想用它的通用視圖MonthArchiveView,把它放在pedido__fecha_pedido不工作你怎麼看到它,所以我不知道我該怎麼做,任何想法?

問候!

編輯與兩種模式:

Pedido

class Pedido(models.Model): 
    referencia = models.CharField(max_length=255) 
    cliente = models.ForeignKey(Cliente,related_name="cliente",on_delete=models.PROTECT) 
    fecha_pedido = models.DateField() 
    fecha_entrega = models.DateField() 
    agencia_envio = models.ForeignKey(Envio, related_name="entrega",blank=True,null=True) 
    producto = models.ManyToManyField(Producto, through='Detalle_Pedido') 
    pendiente_de_factura = models.BooleanField(default=False) 

    def __unicode__(self): 
      return self.referencia 

    def save(self, *args, **kwargs): 
     super(Pedido, self).save(*args,**kwargs) 

    class Meta: 
     ordering = ["referencia","fecha_pedido"] 

Factura

class Factura(models.Model): 
    iva = models.FloatField(default=0.0) 
    pedido = models.ForeignKey(Pedido, related_name="pedido_factura") 

    def __unicode__(self): 
     return "Factura -> ",self.pedido.referencia 

    def save(self, *args, **kwargs): 
     super(Detalle_Pedido, self).save(*args,**kwargs) 

    class Meta: 
     ordering = ["pedido"] 

編輯:回溯錯誤

Environment: 


    Request Method: GET 
    Request URL: http://127.0.0.1:8000/ventas/facturas/2013/8/ 

    Django Version: 1.5.1 
    Python Version: 2.7.3 
    Installed Applications: 
    ('django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.sites', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'widget_tweaks', 
    'dajaxice', 
    'dajax', 
    'suit', 
    'django.contrib.admin', 
    'south', 
    'ventas', 
    'chartit') 
    Installed Middleware: 
    ('django.middleware.common.CommonMiddleware', 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware') 


    Traceback: 
    File "/Users/Tone/blog-env/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 
     115.       response = callback(request, *callback_args, **callback_kwargs) 
    File "/Users/Tone/blog-env/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view 
     25.     return view_func(request, *args, **kwargs) 
    File "/Users/Tone/blog-env/lib/python2.7/site-packages/django/views/generic/base.py" in view 
     68.    return self.dispatch(request, *args, **kwargs) 
    File "/Users/Tone/blog-env/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch 
     86.   return handler(request, *args, **kwargs) 
    File "/Users/Tone/blog-env/lib/python2.7/site-packages/django/views/generic/dates.py" in get 
     333.   self.date_list, self.object_list, extra_context = self.get_dated_items() 
    File "/Users/Tone/blog-env/lib/python2.7/site-packages/django/views/generic/dates.py" in get_dated_items 
     509.    'previous_month': self.get_previous_month(date), 
    File "/Users/Tone/blog-env/lib/python2.7/site-packages/django/views/generic/dates.py" in get_previous_month 
     111.   return _get_next_prev(self, date, is_previous=True, period='month') 
    File "/Users/Tone/blog-env/lib/python2.7/site-packages/django/views/generic/dates.py" in _get_next_prev 
     761.    result = getattr(qs[0], date_field) 

    Exception Type: AttributeError at /ventas/facturas/2013/8/ 
    Exception Value: 'Factura' object has no attribute 'pedido__fecha_pedido' 
+0

你是什麼意思的「不工作」?是否有任何錯誤,列表是空的還是什麼?同時發佈兩個模型定義。 – mariodev

+0

基本上用'pedido__fecha_pedido'我試圖把這個字段用作date_field,但這不起作用,就是這樣,我正在用兩種模型進行編輯以闡明它。 – Enot

回答

0

的原因是,默認情況下,該方法_make_date_lookup_arg使用

model._meta.get_field(self.get_date_field()) 

檢查型號區域,這就是爲什麼你可能會得到「FieldDoesNotExist」。

您可以覆蓋「_make_date_lookup_arg」的方法,使視圖類看起來是這樣的:

class FacturasMonthArchiveView(MonthArchiveView): 
    queryset = Factura.objects.all() 
    date_field = "pedido__fecha_pedido" 
    make_object_list = True 
    allow_future = True 
    template_name = 'ventas/facturas.html' 
    context_object_name = 'facturas_list' 

    def _make_date_lookup_arg(self, value): 
     return value 

雖然我不知道這將如何影響代碼的其餘部分。

+0

對不起,我遲到的答案,我有一段時間,方法'_make_date_lookup_arg'價值我必須返回放在'date_field'之前的字段?在這種情況下,「pedido__fecha_pedido」?我真的不知道如何使它工作,我嘗試了一些組合,總是說沒有領域'pedido__fecha_pedido'或'fecha_pedido' – Enot

+0

@我已經更新了我的答案 – mariodev

+0

這就是我說的,我喜歡你更新它並拋出一個屬性錯誤:''Factura'對象沒有屬性'pedido__fecha_pedido'' – Enot

相關問題