Skip to content

Commit

Permalink
Merge f642734 into 7a4c222
Browse files Browse the repository at this point in the history
  • Loading branch information
kako-nawao committed Jun 21, 2016
2 parents 7a4c222 + f642734 commit 29c26b4
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
12 changes: 10 additions & 2 deletions django_group_by/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,17 @@ def _set_values(self):
pass

else:
# It is a model, build instance from data
# Model, first shorten field name
k = k.replace('__', '_')
v = rel_model(**v)

# Now init instance if required (not if we got ID None)
if 'id' in v and v['id'] is None:
# This means we grouped by ID, if it's none then FK is None
v = None

else:
# Either we have ID or we didn't group by ID, use instance
v = rel_model(**v)

# Set value
setattr(self, k, v)
8 changes: 3 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
name='django_group_by',

# https://packaging.python.org/en/latest/single_source_version.html
version='0.2.1',
version='0.2.2',

description='Group by arbitrary model fields',
long_description=long_description,
Expand Down Expand Up @@ -48,9 +48,7 @@
packages=find_packages(exclude=['contrib', 'docs', 'test_app']),

# https://packaging.python.org/en/latest/requirements.html
install_requires=[
'django>=1.8,<1.9.99'
],
install_requires=[],
extras_require={},
package_data={},

Expand All @@ -63,5 +61,5 @@
entry_points={},

# Django test suite
test_suite='runtests.run_tests'
test_suite='runtests.run_tests',
)
2 changes: 1 addition & 1 deletion test_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Book(models.Model):

class Author(models.Model):
name = models.CharField(max_length=50)
nationality = models.ForeignKey('Nation')
nationality = models.ForeignKey('Nation', null=True)


class Genre(models.Model):
Expand Down
16 changes: 11 additions & 5 deletions test_app/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@ def test_init(self):
with self.assertRaises(AttributeError):
agg.genre

# Provide also related models
# FK None (all fields None, including ID), should not init model
values.update({'author__id': None, 'author__name': None})
agg = AggregatedGroup(Book, values)
self.assertEqual(agg.author, None)

# Change to FK values, without ID
values.pop('author__id')
values.update({'author__name': 'Terry Pratchett', 'genre__name': 'Fantasy'})
agg = AggregatedGroup(Book, values)
self.assertEqual(type(agg.author), Author)
Expand Down Expand Up @@ -92,7 +98,7 @@ def test_group_by(self):
BookFactory.create(author=author1, title='The Light Fantastic')

# Create another book with same title, but different author
author2 = AuthorFactory.create(nationality__name='United States')
author2 = AuthorFactory.create(nationality=None)
BookFactory.create(author=author2, title='The Colour of Magic')

# Group by author, should return two with only author set
Expand Down Expand Up @@ -125,7 +131,8 @@ def test_group_by(self):
group.genre

# Group by nationality, only attr author_nationality is included
res = Book.objects.group_by('author__nationality').order_by('author__nationality').distinct()
# Note: invert order because None goes first
res = Book.objects.group_by('author__nationality').order_by('-author__nationality').distinct()
self.assertEqual(res.count(), 2)
tp, oth = res.all()
self.assertEqual(tp.author_nationality, author1.nationality)
Expand All @@ -134,8 +141,7 @@ def test_group_by(self):
tp.title
with self.assertRaises(AttributeError):
tp.author
self.assertEqual(oth.author_nationality, author2.nationality)
self.assertEqual(oth.author_nationality.name, 'United States')
self.assertEqual(oth.author_nationality, None)
with self.assertRaises(AttributeError):
oth.title
with self.assertRaises(AttributeError):
Expand Down

0 comments on commit 29c26b4

Please sign in to comment.