| 50 | def get_object_or_none(klass, *args, **kwargs): |
| 51 | """ |
| 52 | Uses get() to return an object, or returns an empty QuerySet if the list is empty. |
| 53 | |
| 54 | klass may be a Model, Manager, or QuerySet object. All other passed |
| 55 | arguments and keyword arguments are used in the get() query. |
| 56 | |
| 57 | Note: Like with get(), an MultipleObjectsReturned will be raised if more than one |
| 58 | object is found. |
| 59 | """ |
| 60 | queryset = _get_queryset(klass) |
| 61 | try: |
| 62 | return queryset.get(*args, **kwargs) |
| 63 | except queryset.model.DoesNotExist: |
| 64 | return queryset.none() |
| 65 | |
| 78 | return obj_list |
| 79 | |
| 80 | def get_list_or_none(klass, *args, **kwargs): |
| 81 | """ |
| 82 | Uses filter() to return a list of objects, or return an empty QuerySet if |
| 83 | the list is empty. |
| 84 | |
| 85 | klass may be a Model, Manager, or QuerySet object. All other passed |
| 86 | arguments and keyword arguments are used in the filter() query. |
| 87 | """ |
| 88 | queryset = _get_queryset(klass) |
| 89 | obj_list = list(queryset.filter(*args, **kwargs)) |
| 90 | if not obj_list: |
| 91 | return queryset.none() |