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.
-- 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/-/9tyTV8dq-B8J.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
No comments:
Post a Comment