Skip to content

Commit cf4f07e

Browse files
committed
Make passing the model argument to queryset's __init__ optional. Fixes #241.
1 parent 7cbfd97 commit cf4f07e

3 files changed

Lines changed: 31 additions & 6 deletions

File tree

docs/public/release_notes.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22
Release Notes
33
#############
44

5+
.. release 1.2.1
6+
7+
*****************************
8+
1.2.1 - upcoming release
9+
*****************************
10+
11+
Fixes:
12+
13+
- Make passing the ``model`` argument to queryset's ``__init__`` optional. Still
14+
allow it to be passed either as a positional or named argument — :issue:`241`.
15+
516
.. release 1.2.0
617
718
*****************************

hvad/manager.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,20 @@ class TranslationQueryset(QuerySet):
177177
override_classes[DateTimeQuerySet] = SkipMasterSelectMixin
178178
_skip_master_select = False
179179

180-
def __init__(self, model, *args, **kwargs):
181-
if hasattr(model._meta, 'translations_model'):
182-
# normal creation gets a shared model that we must flip around
183-
model, self.shared_model = model._meta.translations_model, model
184-
elif not hasattr(model._meta, 'shared_model'):
185-
raise TypeError('TranslationQueryset only works on translatable models')
180+
def __init__(self, *args, **kwargs):
181+
# model can be either first positional, or a named arg
182+
if len(args) >= 1:
183+
model, args = args[0], args[1:]
184+
else:
185+
model = kwargs.pop('model', None)
186+
187+
if model is not None: # check the given model is correct
188+
if hasattr(model._meta, 'translations_model'):
189+
# normal creation gets a shared model that we must flip around
190+
model, self.shared_model = model._meta.translations_model, model
191+
elif not hasattr(model._meta, 'shared_model'):
192+
raise TypeError('TranslationQueryset only works on translatable models')
193+
186194
self._local_field_names = None
187195
self._field_translator = None
188196
self._language_code = None

hvad/tests/basic.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from hvad.test_utils.testcase import HvadTestCase, minimumDjangoVersion
1515
from hvad.test_utils.project.app.models import Normal, Unique, Related, MultipleFields, Boolean, Standard
1616
from hvad.test_utils.project.alternate_models_app.models import NormalAlternate
17+
from copy import deepcopy
1718

1819

1920
class DefinitionTests(HvadTestCase):
@@ -162,6 +163,11 @@ def test_options(self):
162163

163164

164165
class QuerysetTest(HvadTestCase):
166+
def test_deepcopy(self):
167+
qs = Normal.objects.language().all()
168+
other = deepcopy(qs)
169+
self.assertEquals(other.model, qs.model)
170+
165171
def test_bad_model(self):
166172
with self.assertRaises(TypeError):
167173
TranslationQueryset(Standard)

0 commit comments

Comments
 (0)