Code

Ticket #13717: 13717_fix_unique_for.diff

File 13717_fix_unique_for.diff, 1.3 KB (added by suzaku, 4 years ago)

Make sure the value of the unique_for field is not None before making the query.

Line 
1Index: db/models/base.py
2===================================================================
3--- db/models/base.py   (revision 13345)
4+++ db/models/base.py   (working copy)
5@@ -798,12 +798,15 @@
6             # there's a ticket to add a date lookup, we can remove this special
7             # case if that makes it's way in
8             date = getattr(self, unique_for)
9-            if lookup_type == 'date':
10-                lookup_kwargs['%s__day' % unique_for] = date.day
11-                lookup_kwargs['%s__month' % unique_for] = date.month
12-                lookup_kwargs['%s__year' % unique_for] = date.year
13+            if date:
14+                if lookup_type == 'date':
15+                    lookup_kwargs['%s__day' % unique_for] = date.day
16+                    lookup_kwargs['%s__month' % unique_for] = date.month
17+                    lookup_kwargs['%s__year' % unique_for] = date.year
18+                else:
19+                    lookup_kwargs['%s__%s' % (unique_for, lookup_type)] = getattr(date, lookup_type)
20             else:
21-                lookup_kwargs['%s__%s' % (unique_for, lookup_type)] = getattr(date, lookup_type)
22+                lookup_kwargs[unique_for] = None
23             lookup_kwargs[field] = getattr(self, field)
24 
25             qs = model_class._default_manager.filter(**lookup_kwargs)