slowly getting there thanks to your help. I am actually trying to accomplish this in the Admin interface, so I am not sure how to use the template tag {{ ticket.days_old }} in that situation.
the other part I left off yesterday under my model I then had.. (trying to get code formatting correct but keeps going to the left margin on me when i post) def days_old(self): return self.objects.datecalc() days_old.short_discription = 'Days Old' more or less is that a correct way I would pull in a custom manager, lets say if this one didn't have the iteration to it which seems be to be my problem part now. thanks On Oct 10, 2:00 pm, Daniel Roseman <dan...@roseman.org.uk> wrote: > On Monday, 10 October 2011 19:14:51 UTC+1, eyscooby wrote: > > > On Oct 5, 3:11 am, Daniel Roseman <dan...@roseman.org.uk> wrote: > > > On Wednesday, 5 October 2011 01:27:54 UTC+1, eyscooby wrote: > > > > > new to django/python developement, can't get this one figured out. > > > > > I have a model that has a couple DateFields (issued_date & > > > > completion_date), and I'm trying to return a value with the difference > > > > of the two on each entry that is complete, and then if it isn't > > > > completed yet, show the amount of days since it was issued. > > > > I am using the Admin interface and what I have in the model is > > > > this.... > > > > > models.py > > > > class RequestTicket(models.Model): > > > > . . . > > > > issued_date = DateField() > > > > completed_date = DateField(blank=True, null=True) > > > > > def days_old(self): > > > > complete = RequestTicket.object.filter(completion_date__isnull=False) > > > > for ticket in complete: > > > > return ticket.completion_date - ticket.issued_date > > > > return date.today() - self.issued.date > > > > days_old.short_discription = 'Days Old' > > > > > what i get returned is if the first entry was completed within 2 days > > > > (issued=9/14, completed=9/16), all entries after that get 2 days, even > > > > if there is no completion date. > > > > If i use 'self.object.filter(completion_date__isnull=False)', I get a > > > > NONE answer on all entries > > > > If I don't filter for just completed entries I get an error trying to > > > > subtract NoneType field with DateField, i guess that might be from the > > > > NULL setting. > > > > Any help, advice would be great, or if i need to post in another area. > > > > > Django version 1.2 > > > > > thanks > > > > Kenney > > > > OK, there are a few things wrong with your `days_old` function. > > > > Firstly, it operates on a queryset, not an instance, so it should be a > > > method of the Manager, not the Model. > > > > Secondly, you can't return multiple times like that. You can only return > > > once from a function. You need to build up a list of values, and return > > that > > > - or set the attribute on each element of the queryset. > > > -- > > > DR.- Hide quoted text - > > > > - Show quoted text - > > > DR. > > would creating a manager something like this work with a list?? > > > class RequestTicketManager(models.Manager): > > def datecalc(self): > > complete_list = > > list(self.objects.filter(competion_date__isnull=False)) > > for day in complete_list: > > return day.competion_date - day.issued_date > > > I then put "objects = RequestTicketManager()" in my model > > > thanks for you help, > > You're almost there, but you're still trying to return multiple times. The > last few lines should be: > > for day in complete_list: > day.days_old = day.completion_date - day.issued_date > return complete_list > > Actually, since that calculation is fairly trivial, you could probably do it > as an instance method on the model class: > > class RequestTicket(models.Model): > ... > def days_old(self): > return self.completion_date - self.issued_date > > The difference between this and the method you first proposed is that this > only acts on a single instance at a time - so you'll need to get the > queryset of completed items in your view in the normal way, then when you > iterate through in the template you can just do {{ ticket.days_old }}. > -- > DR.- Hide quoted text - > > - Show quoted text - -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.