0

我有如下:

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'

pedido在表Factura中有一个字段引用了包含许多信息的订单,其中一个字段是fecha_pedido我想将它用于通用视图MonthArchiveView,把它放在上面pedido__fecha_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"]

法图拉

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

1 回答 1

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

虽然我不知道这将如何影响其余代码。

于 2013-08-19T11:54:56.327 回答