On Monday, 17 October 2011 20:28:47 UTC+1, eyscooby wrote:
>
> Ok, sorry I thought I was starting to understand it a little better,
> but now I think I took a step backwards, so if it is ok with you let's
> step back and take it a step at a time.
>
> So, my first step is wondering if I really need a manager or not??
> I was thinking from your first response to me that you might be
> suggesting that I did. Let's start there.
> If I understand it correctly the Manager is a way of retrieving
> specific information.
>
> thanks
>
Sorry for confusing you. There are two things going on here.
A Manager is for making custom queries to the database, to return new
objects - either one or a queryset of many. Your original code was using
`Model.filter()` and modifying the result, so I suggested that it belonged
in a manager.
A model method is useful when you want to do a separate, non-database,
operation on a single object. That's what you really want to do here - given
an instance of RequestTicket, calculate how old it is. There's no iteration
contained in the method - you iterate through your existing queryset
elsewhere (say in the template) and call days_old on each instance:
{% for ticket in completed_tickets %}
{{ ticket.name }}: {{ ticket.days_old }}
{% endif %}
Or, in your particular circumstance, you simply give the `days_old` method
as one of the elements of the `list_display` tuple, and Django takes care of
the iterating, calling `days_old` on each row in the changelist.
So in both of these circumstances, `days_old` simply needs to look like
this:
def days_old(self):
return self.competion_date - self.issued_date
- so it returns a single value, for the one particular ticket instance which
it has been called on.
Hope that helps.
--
DR.
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/django-users/-/8Mn1EJ4QdtAJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.