0

I have Topiccenter Model:

class TopicCenter(models.Model):
  title = models.TextField()      
  def latest_entry(self):
    latest_entries = []
    book = self.tc_books.order_by('-id')[:1]
    journal = self.tc_journals.order_by('-id')[:1]
    if book: 
        for b in book:
            if b:
                latest_entries.append(b)
    if journal: 
        for jn in journal: 
            if jn: 
                latest_entries.append(jn)
    lastone = []
    if latest_entry:            
        lastone = max(latest_entries, key = lambda x: x.added)
    return lastone
    # what to return here if lastone is empty list ?? :(

each topiccenter can have many books and journals. I want to get the latest entry by its added field.

I am sorting now topiccenters by the date of its latest entry. Now the Problem i am facing is, some topiccenters are totally empty (no book, no journals) so i dont know what to return in latest_entry() method if latest_entry is [] so that I can use it here like this:

tcss = TopicCenter.objects.all().distinct('id')  
sorter = lambda x: x.latest_entry().added
tcs = sorted(tcss, key=sorter, reverse=True)

at this moment i am getting 'list' object has no attribute 'added' because one Topiccenter has neither book nor journal, so latest_entry() is returning [] which causing the error message.

can someone please help me how to solve this logic :(

4
  • 1
    You can filter out topics with no entries. Commented Jun 10, 2014 at 12:45
  • @haki but i need to show also topics which dont have any entry with entry=0 Commented Jun 10, 2014 at 14:04
  • Then you need to decide the way as those empty-entry topics to be placed in the sorted result. Commented Jun 10, 2014 at 14:34
  • @okm good point thanks Commented Jun 10, 2014 at 15:08

2 Answers 2

1

I assume you are using latest_entry() in other places too, so just create another method latest_added_time() which returns latest_entry.added or a fake time.

class TopicCenter(models.Model):
    ...
    def latest_added_time(self):
        latest = self.latest_entry()
        if latest:
            return latest.added
        else:
            # returns a time to place it at the end of the sorted list
            return datetime(1, 1, 1) 

Then, you can just sort on this new method:

tcss = TopicCenter.objects.all().distinct('id')  
sorter = lambda x: x.latest_added_time()
tcs = sorted(tcss, key=sorter, reverse=True)

If you aren't using latest_entry() for anything else, then you should just put this logic directly into that function.

Sign up to request clarification or add additional context in comments.

Comments

0

You can try by changing your conditions

if not book and not journal:
    #Action you want to perform

or you can see if lastone list is empty you can append any message there like

if not len(lastone):
    #Your code to append a message

1 Comment

Thanks but this is not what i ask here. It should be sorted properly with what i am returning if there is no entry.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.