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

Does not work with django-taggit #10

Closed
philipmat opened this issue Feb 11, 2013 · 12 comments · Fixed by #86
Closed

Does not work with django-taggit #10

philipmat opened this issue Feb 11, 2013 · 12 comments · Fixed by #86

Comments

@philipmat
Copy link

Django-reversion-compare seems to have a good deal of problems with django-taggit; perhaps it's due to django-taggit not being up-to-date enough with Django 1.4.5, but I tried both yedpodtrzitko's and shakirjames's and both exhibit the same issue.

Django 1.4.5 with a model as following:

class Argument(models.Model):
    tags = TaggableManager(through=TaggedTags)
    tags.rel.related_name = 'tags'

django-reversion-compare throws a KeyError at reversion_compare/admin.py:48:

self.value = version.field_dict[field_name] 

Changing this line to version.field_dict.get(field_name, None) makes it go down a rabbit-hole of issues with TaggableManager not providing get_internal_type which d-r-c makes use of frequently, eg:

# Line 81
assert self.field.get_internal_type() != "ManyToManyField" 
# Line 225
if self.field.get_internal_type() == "ManyToManyField":
# etc

Is the right solution to check in all places where get_internal_type is used to ensure the field hasattr(field, 'get_internal_type')?

@jedie
Copy link
Owner

jedie commented Feb 12, 2013

Thanks for reporting this!

Hm. I have used django-tagging in the past. But this is also as django-taggit not really up to date :(

Maybe django-tagging-ng is the currently the only maintained tag solution? See: https://www.djangopackages.com/grids/g/tagging/

So, i question me, how django-tagging-ng works with reversion-compare...

@philipmat
Copy link
Author

I've patched my local admin.py file to check for hasattr(..., 'get_internal_type') and at least it seems to get past the errors, even though it doesn't display the history for tags.
Would you consider a patch with these changes?
I'll give a shot to django-tagging-ng, but meanwhile, given the popularity of django-taggit, it will at least not cause crashes when django-taggit and django-reversion-compare are used together.

@jedie
Copy link
Owner

jedie commented Mar 22, 2013

Seems that's a general problem, see: #12

@odinho
Copy link

odinho commented Mar 30, 2013

This also happened for my code. I solved it by changing it to:

self.value = ''
if field_name in version.field_dict:
    self.value = version.field_dict[field_name] 

@jedie
Copy link
Owner

jedie commented Apr 1, 2013

@velmont: IMHO this should do the same:

self.value = version.field_dict.get(field_name, "")

In the next few weeks i have no time to test this and create a unittest...

@odinho
Copy link

odinho commented Apr 1, 2013

Absolutely, it's a much better pattern and I have no idea how I could've missed it.

I did fix it two places though, and the second virtualenv I fixed it in I indeed did the get version :-) I am not sure if it is the right fix to set the value to the empty string if nothing is found, but it does sound correct.

What do you think? Is it the real fix? I have no time right now, but if it is the real one, someone else might do a test and fix, -- or I might be able to do one during the week.

odinho added a commit to odinho/django-reversion-compare that referenced this issue Oct 19, 2013
@jedie
Copy link
Owner

jedie commented Jul 22, 2015

Any news on this issues? Did this happen in the current release or can this been closed?!?

@odinho
Copy link

odinho commented Jul 22, 2015

I'm using my fork as dependency in the project where I'm doing this, haven't checked the new version. Don't have the data it crashes on handy, so it might take me some time to figure out the answer to that question. But based on reading the code that's now in admin.py, it seems like it would work (you've seemed to have implemented the fix, but with "Field doesn't exist!" as value instead of "").

@murdav
Copy link

murdav commented Aug 25, 2016

Dear All,

Using:

Django=1.9.9
django-taggit==0.20.2
django-reversion==2.0.6
django-reversion-compare==0.7.0

I got this error in compare.py line 155:
ids = frozenset(map(force_text, self.value)) because self.value is a taggit.managers._TaggableManager object

Exception Type: TypeError
Exception Value: '_TaggableManager' object is not iterable

Ideas?
Thanks,

D

@sadnoodles
Copy link

sadnoodles commented Sep 1, 2017

A monkey patch for this issue:

from reversion_compare.mixins import CompareMixin
from django.db.models import Manager


_old_compare = CompareMixin.compare


def compare(self, obj, version1, version2):
    def replace_taggit_field(version_ins):
        for fieldname in version_ins.field_dict:
            if isinstance(version_ins.field_dict[fieldname], Manager):
                version_ins.field_dict[fieldname] = []
    replace_taggit_field(version1)
    replace_taggit_field(version2)
    return _old_compare(self, obj, version1, version2)


CompareMixin.compare = compare

Put those code at the start lines of your admin.py.

@jedie
Copy link
Owner

jedie commented Sep 1, 2017

@sadnoodles can you contribute a pull request with a real fix?

@sadnoodles
Copy link

@jedie I'm glad to, but I'm not sure this is the solution. This patch works for me. Basic its just a custom field problem. And in this case, in taggit, it is a M2M Manager, some may be more strange. I'm thinking a presetting function that can be customized would be a good idea, but I not sure where to put it for now.

@jedie jedie closed this as completed in #86 Dec 6, 2017
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

Successfully merging a pull request may close this issue.

5 participants