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.

-- 
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/-/O9adLo_RkHEJ.
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.

Reply via email to