Skip to content

Commit

Permalink
Merge pull request #4 from jokiefer/bugfix/#3-None-valued-id
Browse files Browse the repository at this point in the history
fixes #3
  • Loading branch information
jokiefer authored Oct 17, 2024
2 parents f799a93 + 4cce1d1 commit a1646d8
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


[0.3.1] - 2024-10-17
--------------------

Fixed
~~~~~
* None valued ids on bulk add handling


[0.3.0] - 2023-07-10
--------------------

Expand Down
2 changes: 1 addition & 1 deletion atomic_operations/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = "0.3.0"
__version__ = "0.3.1"
VERSION = __version__ # synonym
5 changes: 3 additions & 2 deletions atomic_operations/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,11 @@ def perform_bulk_create(self, bulk_operation_data):
_serializer.is_valid(raise_exception=True)
instance = model_class(**_serializer.validated_data)
objs.append(instance)
self.response_data.append(
_serializer.__class__(instance=instance).data)
model_class.objects.bulk_create(
objs)
# append serialized data after save has successfully called. Otherwise id could be None. See #3
self.response_data.extend(
[_serializer.__class__(instance=obj).data for obj in objs])

def perform_bulk_delete(self, bulk_operation_data):
obj_ids = []
Expand Down
12 changes: 10 additions & 2 deletions tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json

from django import VERSION
from django.test import Client, RequestFactory, TestCase

from atomic_operations.consts import (
Expand Down Expand Up @@ -195,8 +196,15 @@ def test_view_processing_with_valid_request(self):

self.assertEqual(RelatedModel.objects.get(pk=1),
BasicModel.objects.get(pk=2).to_one)
self.assertQuerysetEqual(RelatedModelTwo.objects.filter(pk__in=[1, 2]),
BasicModel.objects.get(pk=2).to_many.all())

major, minor, _, _, _ = VERSION
if int(major) <= 4 and int(minor) <= 1:
self.assertQuerysetEqual(RelatedModelTwo.objects.filter(pk__in=[1, 2]),
BasicModel.objects.get(pk=2).to_many.all())
else:
# with django 4.2 TransactionTestCase.assertQuerysetEqual() is deprecated in favor of assertQuerySetEqual().
self.assertQuerySetEqual(RelatedModelTwo.objects.filter(pk__in=[1, 2]),
BasicModel.objects.get(pk=2).to_many.all())

def test_bulk_view_processing_with_valid_request(self):
operations = [
Expand Down

0 comments on commit a1646d8

Please sign in to comment.