jazzband / django-recurrence

Utility for working with recurring dates in Django.
https://django-recurrence.readthedocs.org/
BSD 3-Clause "New" or "Revised" License
491 stars 190 forks source link

Recommended way to check if any recurrences have been defined in the field #178

Closed BigglesZX closed 3 years ago

BigglesZX commented 3 years ago

Hi, thanks for this super useful library.

I can filter quite easily to find models that do/don't have recurrences defined in their RecurrenceField, e.g. by using .filter(recurrences='') or .exclude(recurrences='') in QuerySets (where the RecurrenceField is called recurrences).

However when accessing a specific model instance, I can't work out how to cheaply check if any recurrences are defined on its field. Inspecting instance.recurrences.count() works, but can be extremely expensive for the reasons noted in the documentation.

Am I missing something obvious? Is there a comparator or method on the field I can use?

Thanks!

BigglesZX commented 3 years ago

Turns out I had already solved this, and forgotten.

The solution is a simple boolean test, e.g.

if instance.recurrences:
    # some recurrences are defined
else:
    # no recurrences are defined