Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support Django 2.2 #130

Closed
kennir opened this issue Apr 14, 2019 · 14 comments
Closed

support Django 2.2 #130

kennir opened this issue Apr 14, 2019 · 14 comments

Comments

@kennir
Copy link

kennir commented Apr 14, 2019

Modify sortedm2m field will raise a exception after upgrade to Django 2.2

TypeError: _add_items() got an unexpected keyword argument 'through_defaults'

How can I fix it

@pleasedontbelong
Copy link

pleasedontbelong commented Apr 18, 2019

I changed the code of _add_items https://github.com/gregmuellegger/django-sortedm2m/blob/master/sortedm2m/fields.py#L95 to:

def _add_items(self, source_field_name, target_field_name, *objs, through_defaults=None):
        # source_field_name: the PK fieldname in join table for the source object
        # target_field_name: the PK fieldname in join table for the target object
        # *objs - objects to add. Either object instances, or primary keys of object instances.
        through_defaults = through_defaults or {}

        # If there aren't any objects, there is nothing to do.
        from django.db.models import Max, Model
        if objs:
            # Django uses a set here, we need to use a list to keep the
            # correct ordering.
            new_ids = []
            for obj in objs:
                if isinstance(obj, self.model):
                    if not router.allow_relation(obj, self.instance):
                        raise ValueError(
                            'Cannot add "%r": instance is on database "%s", value is on database "%s"' %
                            (obj, self.instance._state.db, obj._state.db)
                        )
                    fk_val = self.through._meta.get_field(
                        target_field_name).get_foreign_related_value(obj)[0]
                    if fk_val is None:
                        raise ValueError(
                            'Cannot add "%r": the value for field "%s" is None' %
                            (obj, target_field_name)
                        )
                    new_ids.append(fk_val)
                elif isinstance(obj, Model):
                    raise TypeError(
                        "'%s' instance expected, got %r" %
                        (self.model._meta.object_name, obj)
                    )
                else:
                    new_ids.append(obj)

            db = router.db_for_write(self.through, instance=self.instance)
            manager = self.through._default_manager.using(db)
            vals = (manager
                    .values_list(target_field_name, flat=True)
                    .filter(**{
                        source_field_name: self._fk_val,
                        '%s__in' % target_field_name: new_ids,
                    }))
            for val in vals:
                if val in new_ids:
                    new_ids.remove(val)
            _new_ids = []
            for pk in new_ids:
                if pk not in _new_ids:
                    _new_ids.append(pk)
            new_ids = _new_ids
            new_ids_set = set(new_ids)

            if self.reverse or source_field_name == self.source_field_name:
                # Don't send the signal when we are inserting the
                # duplicate data row for symmetrical reverse entries.
                signals.m2m_changed.send(sender=rel.through, action='pre_add',
                    instance=self.instance, reverse=self.reverse,
                    model=self.model, pk_set=new_ids_set, using=db)

            # Add the ones that aren't there already
            with atomic(using=db):
                fk_val = self._fk_val
                source_queryset = manager.filter(**{'%s_id' % source_field_name: fk_val})
                sort_field_name = self.through._sort_field_name
                sort_value_max = source_queryset.aggregate(max=Max(sort_field_name))['max'] or 0

                manager.bulk_create([
                    self.through(**through_defaults, **{
                        '%s_id' % source_field_name: fk_val,
                        '%s_id' % target_field_name: pk,
                        sort_field_name: sort_value_max + i + 1,
                    })
                    for i, pk in enumerate(new_ids)
                ])

            if self.reverse or source_field_name == self.source_field_name:
                # Don't send the signal when we are inserting the
                # duplicate data row for symmetrical reverse entries.
                signals.m2m_changed.send(sender=rel.through, action='post_add',
                    instance=self.instance, reverse=self.reverse,
                    model=self.model, pk_set=new_ids_set, using=db)

it seems to work but it's not ready to be a PR

@MisterGlass
Copy link

I am also running into this issue. Are there any updates? I see a few PRs attempting to address it, they all seem to fail CI due to an issue with the CI build

@npadgen
Copy link

npadgen commented May 7, 2019

Looking at issue #112 I think @gregmuellegger may have abandoned this project. Could someone fork the project? My project depends heavily on this library!

@MisterGlass
Copy link

Maybe we should reach out on twitter or email & ask what they would prefer? There is contact info in the project README

@npadgen
Copy link

npadgen commented May 7, 2019

Maybe we should reach out on twitter or email & ask what they would prefer? There is contact info in the project README

I’ve just pinged him on twitter.

@MisterGlass
Copy link

@npadgen Did the maintainer respond?

@vprinz
Copy link

vprinz commented Jun 4, 2019 via email

@ppawlak
Copy link

ppawlak commented Jun 10, 2019

Hello,

Considering the original author of this package had been completely silent for a long time I decided to fork this repo and publish the package under a new name on PyPI.

I could not come up with a good name so I just named it django-sorted-m2m (note the additional -). I hope it does not create too much confusion. If someone has a better idea, please let me know.

The forked repo includes pull request #134 by @joehybird to support Django 2.2. Kudos to him!

My repo is at: https://github.com/Ponytech/django-sorted-m2m
PyPI link: https://pypi.org/project/django-sorted-m2m

Anyone interested into taking part in this fork is more than welcome!

@richardbarran
Copy link
Contributor

FYI I have asked Jazzband about the possibility of moving this project over to their community, even if @gregmuellegger doesn't answer pings.

@vprinz
Copy link

vprinz commented Jun 23, 2019 via email

@richardbarran
Copy link
Contributor

Greg has answered!
See this thread.

@vprinz
Copy link

vprinz commented Jun 26, 2019 via email

@chadsaun
Copy link

So now that Greg answered, is anyone able to move in pull request #134 by @joehybird?

@kennir kennir closed this as completed Jul 29, 2019
@kegan
Copy link

kegan commented Jul 29, 2019

Will there be a release soon?

iDevPy added a commit to iDevPy/django-sortedm2m that referenced this issue Aug 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants