I get error in using .add() and .set() functions with many to many keys in
models.
I rolled back to Django-2.2.12 and this issue was gone.
{{{
class GroupSerializer(serializers.ModelSerializer):
permissions = PermissionSerializerGroup(many=True, required=False)
class Meta:
model = Group
fields = ('id', 'name', 'permissions')
extra_kwargs = {
'name': {'validators': []},
}
def create(self, validated_data):
print(validated_data)
permissions_data = validated_data.pop("permissions")
obj, group =
Group.objects.update_or_create(name=validated_data["name"])
obj.permissions.clear()
for permission in permissions_data:
per = Permission.objects.get(codename=permission["codename"])
obj.permissions.add(per)
obj.save()
return obj
}}}
Traceback:
{{{
File "/home/suman/Desktop/suman1234/myvenv/lib/python3.6/site-
packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/suman/Desktop/suman1234/myvenv/lib/python3.6/site-
packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/suman/Desktop/suman1234/myvenv/lib/python3.6/site-
packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args,
**callback_kwargs)
File "/home/suman/Desktop/suman1234/myvenv/lib/python3.6/site-
packages/django/contrib/admin/options.py", line 607, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/suman/Desktop/suman1234/myvenv/lib/python3.6/site-
packages/django/utils/decorators.py", line 130, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/suman/Desktop/suman1234/myvenv/lib/python3.6/site-
packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/home/suman/Desktop/suman1234/myvenv/lib/python3.6/site-
packages/django/contrib/admin/sites.py", line 231, in inner
return view(request, *args, **kwargs)
File "/home/suman/Desktop/suman1234/myvenv/lib/python3.6/site-
packages/django/contrib/admin/options.py", line 1638, in add_view
return self.changeform_view(request, None, form_url, extra_context)
File "/home/suman/Desktop/suman1234/myvenv/lib/python3.6/site-
packages/django/utils/decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "/home/suman/Desktop/suman1234/myvenv/lib/python3.6/site-
packages/django/utils/decorators.py", line 130, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/suman/Desktop/suman1234/myvenv/lib/python3.6/site-
packages/django/contrib/admin/options.py", line 1522, in changeform_view
return self._changeform_view(request, object_id, form_url,
extra_context)
File "/home/suman/Desktop/suman1234/myvenv/lib/python3.6/site-
packages/django/contrib/admin/options.py", line 1566, in _changeform_view
self.save_related(request, form, formsets, not add)
File "/home/suman/Desktop/suman1234/myvenv/lib/python3.6/site-
packages/django/contrib/admin/options.py", line 1107, in save_related
form.save_m2m()
File "/home/suman/Desktop/suman1234/myvenv/lib/python3.6/site-
packages/django/forms/models.py", line 442, in _save_m2m
f.save_form_data(self.instance, cleaned_data[f.name])
File "/home/suman/Desktop/suman1234/myvenv/lib/python3.6/site-
packages/django/db/models/fields/related.py", line 1618, in save_form_data
getattr(instance, self.attname).set(data)
File "/home/suman/Desktop/suman1234/myvenv/lib/python3.6/site-
packages/django/db/models/fields/related_descriptors.py", line 1008, in
set
self.add(*new_objs, through_defaults=through_defaults)
File "/home/suman/Desktop/suman1234/myvenv/lib/python3.6/site-
packages/django/db/models/fields/related_descriptors.py", line 946, in add
through_defaults=through_defaults,
File "/home/suman/Desktop/suman1234/myvenv/lib/python3.6/site-
packages/django/db/models/fields/related_descriptors.py", line 1129, in
_add_items
], ignore_conflicts=True)
TypeError: _bulk_create() got an unexpected keyword argument
'ignore_conflicts'
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/31551>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* status: new => closed
* resolution: => needsinfo
Comment:
Thanks for this ticket, however I cannot reproduce this issue, moreover
`_bulk_create()` (with leading underscore) never existed in the Django.
Can you provide a sample project to reproduce this issue?
--
Ticket URL: <https://code.djangoproject.com/ticket/31551#comment:1>
* status: closed => new
* version: 3.0 => 3.1
* resolution: needsinfo =>
Comment:
I am having the same issue in 3.1. This happens when calling .add() both
from Django project files and from the shell. You should be able to
reproduce it in this project:
https://github.com/emeanor/wtp/
Specifically, the error is being generated in /texts/tests.py, by the
function {{{test_can_be_attached_to_multiple_divisions}}} (currently
commented out):
https://github.com/emeanor/wtp/blob/main/texts/tests.py
{{{
def test_can_be_attached_to_multiple_divisions(self):
edition = Edition.objects.create()
divisions = [Division.objects.create(edition=edition) for i in
range(3)]
for division in divisions:
division.texts.add(self.text) # .add() is the error source.
self.assertEquals(len(divisions), self.text.division_set.count())
for division in divisions:
self.assertIn(division, self.text.division_set)
}}}
Trackeback:
{{{
Traceback (most recent call last):
File "/home/ethan/dev/projects/wtp/texts/tests.py", line 41, in
test_can_be_attached_to_multiple_divisions
division.texts.add(self.text)
File "/home/ethan/dev/.venv/wtp/lib/python3.8/site-
packages/django/db/models/fields/related_descriptors.py", line 950, in add
self._add_items(
File "/home/ethan/dev/.venv/wtp/lib/python3.8/site-
packages/django/db/models/fields/related_descriptors.py", line 1150, in
_add_items
self.through._default_manager.using(db).bulk_create([
TypeError: bulk_create() got an unexpected keyword argument
'ignore_conflicts'
}}}
Note that {{{bulk_create()}}} here does not have the leading underscore.
--
Ticket URL: <https://code.djangoproject.com/ticket/31551#comment:2>
* status: new => closed
* resolution: => invalid
Comment:
That looks like an issue with
[https://github.com/bfirsh/django-ordered-model/pull/228 django-ordered-
model]. When providing steps to reproduce an issue, try to remove all
third-party dependencies to rule out a bug there.
--
Ticket URL: <https://code.djangoproject.com/ticket/31551#comment:3>