def order_by_next_occurrence(self):
        """
        
        :return: A list of events in order of minimum occurrence greater than 
        now (or overlapping now in the case of drop-in events). 
        
        This is an expensive operation - use with as small a source 
        queryset as possible.
        
        Events with no upcoming occurrence appear last (in order of their first 
        occurrence). Events with no occurrences at all appear right at the end 
        (in no order). To remove these, use "with_upcoming_occurrences" or 
        "with_upcoming_or_no_occurrences".
        """

        qs = self.prefetch_related('occurrences')
        def _sort(x):
            try:
                # If there's an upcoming occurrence, use it.
                return x.get_next_occurrence().start
            except AttributeError:
                try:
                    # If there's any occurrence, use the first one, plus 1000 years.
                    return x.get_first_occurrence().start + timedelta(days=1000*365)
                except AttributeError:
                    # If no occurence, use a localised datetime.max (minus a
                    # few days to avoid overflow)
                    return make_aware(datetime.max-timedelta(2))

        sorted_qs = sorted(qs, key=_sort)

        return sorted_qs
def order_by_next_occurrence ( self ) : qs = self . prefetch_related ( 'occurrences' ) def _sort ( x ) : try : return x . get_next_occurrence ( ) . start except AttributeError : try : return x . get_first_occurrence ( ) . start + timedelta ( days = 1000 * 365 ) except AttributeError : return make_aware ( datetime . max - timedelta ( 2 ) ) sorted_qs = sorted ( qs , key = _sort ) return sorted_qs
:return: A list of events in order of minimum occurrence greater than 
        now (or overlapping now in the case of drop-in events). 
        
        This is an expensive operation - use with as small a source 
        queryset as possible.
        
        Events with no upcoming occurrence appear last (in order of their first 
        occurrence). Events with no occurrences at all appear right at the end 
        (in no order). To remove these, use "with_upcoming_occurrences" or 
        "with_upcoming_or_no_occurrences".