Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » getting back max date from a joined table for list_display [RSS Feed]

#1 Dec. 16, 2010 12:40:17

dan k.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

getting back max date from a joined table for list_display


Hi, I'm new to python and django and need to join two tables together
- the first table holds details about a customer and I think is
probably passed as the obj parameter of the following function. The
second is a CallBackRequest table that holds details of when I should
next call a customer. The callback has a foreign key (customer_id) to
the customer table. I want the customers and their next callback
(based on max date) to be displayed in a list - see the list_display
line. Note, there's a one-to-many relationship between customer and
callbackrequest records.

The details in the customer table are retrieved and displayed
successfully in the list but the date of the next call back is blank.
I'm almost certain it's to do with the filter but not sure how to join
the foreign key of the callbackrequest with the primary key of the obj
record. Almost certainly the pk reference is wrong as it's the foreign
key I need anyway. The foreign key within callbackrequests is called
customer_id.

The (wrong) code I currently have is as follows:

def next_call_back_due(self, obj):

max_call_record = CallBackRequest.objects.filter(customer_id =
obj.id).filter(call_back_date=CallBackRequest.objects.filter(customer_id=obj.id).aggregate(Max('call_back_date'))
)

callback_date = max_call_record.call_back_date

return callback_date

list_display = ('firstname', 'surname', 'telephone_number',
'next_call_back_due')

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Dec. 16, 2010 15:40:32

Michael D.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

getting back max date from a joined table for list_display


On Dec 16, 2010, at 3:53 AM, dan kirkland wrote:

> Hi, I'm new to python and django and need to join two tables together
> - the first table holds details about a customer and I think is
> probably passed as the obj parameter of the following function. The
> second is a CallBackRequest table that holds details of when I should
> next call a customer. The callback has a foreign key (customer_id) to
> the customer table. I want the customers and their next callback
> (based on max date) to be displayed in a list - see the list_display
> line. Note, there's a one-to-many relationship between customer and
> callbackrequest records.
>
> The details in the customer table are retrieved and displayed
> successfully in the list but the date of the next call back is blank.
> I'm almost certain it's to do with the filter but not sure how to join
> the foreign key of the callbackrequest with the primary key of the obj
> record. Almost certainly the pk reference is wrong as it's the foreign
> key I need anyway. The foreign key within callbackrequests is called
> customer_id.

What about this?

def next_call_back_due(self, obj):
call_records =
CallbackRequest.objects.filter(customer_id=obj.id).order_by('-call_back_date')
if call_records.count() > 0:
return call_records.call_back_date
else:
return None # Or whatever default you want to return



----
Michael Dippery
mdipp...@gmail.com | www.monkey-robot.comsmime.p7sDescription:S/MIME cryptographic signature

Attachments:
attachment smime.p7s (3.6 KB)

Offline

  • Root
  • » Django
  • » getting back max date from a joined table for list_display [RSS Feed]

Board footer

Moderator control

Enjoy the 17th of August
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support