0

I have as follows:

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'

I have a field pedidoin table Factura that references to an order with the many information, one of that fields is fecha_pedidothat I want use it for the generic view MonthArchiveView, put it on pedido__fecha_pedido doesn't work how you can see it so I don't know how can I do that, any ideas?

Regards!

Edit with the two models:

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"]

Edit: Traceback error

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'
Enot
  • 790
  • 2
  • 15
  • 34
  • what do you mean by "doesn't work"? is there any error, the list is empty or what? Also post both model definitions. – mariodev Aug 18 '13 at 19:12
  • Basically with `pedido__fecha_pedido`I'm trying to make a reference to this field for use as a date_field but this doesn't work, that's it, I'm edit with the two models for clarify it. – Enot Aug 18 '13 at 21:26

1 Answers1

0

The reason is that by default the method _make_date_lookup_arg uses

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

to check for the model field, that's why you probably get "FieldDoesNotExist".

You can override "_make_date_lookup_arg" method, so that view class looks like this:

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

although I don't know how will this affect the rest of the code.

mariodev
  • 13,928
  • 3
  • 49
  • 61
  • Sorry for my late answer, I was out some days, for method `_make_date_lookup_arg` for value I have to return the field put in `date_field` before? `pedido__fecha_pedido` in this case? I don't really know how to make it work, I tried with some combinations and always says there's no field `pedido__fecha_pedido`or `fecha_pedido` – Enot Aug 25 '13 at 10:55
  • That's what I said, I had like you updated it and throws me an attribute error: `'Factura' object has no attribute 'pedido__fecha_pedido'` – Enot Aug 25 '13 at 11:24
  • @Enot does it throw the same error before and after overriding this method? Can you post the whole traceback somewhere? – mariodev Aug 25 '13 at 11:56
  • @Enot OK, so I guess it is not possible, when you use 'previous_month' template variable, it gets date attribute internally, which in this case is hard to override (and it's better not to). – mariodev Aug 25 '13 at 15:18