dominno / django-moderation

django-moderation is reusable application for Django framework, that allows to moderate any model objects.
BSD 3-Clause "New" or "Revised" License
269 stars 90 forks source link

Unable to import fixtures on model registered with moderation. #11

Closed hexabits closed 14 years ago

hexabits commented 14 years ago

I tried running manage syncdb (UPDATE: It was actually manage test) and received a ~3400 line traceback...

Most of it is a repeating pattern relating to moderation, I've cut out the pattern and just left one, but the pattern repeats for over 3000 lines...

Problem installing fixture:

  File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 169, in handle
    obj.save(using=using)
  File "/usr/local/lib/python2.7/site-packages/django/core/serializers/base.py", line 165, in save
    models.Model.save_base(self.object, using=using, raw=True)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 459, in save_base
    signals.pre_save.send(sender=origin, instance=self, raw=raw)
  File "/usr/local/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 166, in send
    response = receiver(signal=self, sender=sender, **named)

  ## Start pattern

  File "/usr/local/lib/python2.7/site-packages/moderation/__init__.py", line 245, in pre_save_handler
    moderated_object.save()
  File "/usr/local/lib/python2.7/site-packages/moderation/models.py", line 77, in save
    auto_save=False)
  File "/usr/local/lib/python2.7/site-packages/moderation/models.py", line 130, in approve
    auto_save)
  File "/usr/local/lib/python2.7/site-packages/moderation/models.py", line 113, in _moderate
    self.changed_object.save()
  File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 435, in save
    self.save_base(using=using, force_insert=force_insert, force_update=force_update)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 459, in save_base
    signals.pre_save.send(sender=origin, instance=self, raw=raw)
  File "/usr/local/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 166, in send
    response = receiver(signal=self, sender=sender, **named)

  ## End pattern

  File "/usr/local/lib/python2.7/site-packages/moderation/__init__.py", line 257, in _get_or_create_moderated_object
    moderated_object = ModeratedObject.objects.get(object_pk=pk)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/manager.py", line 132, in get
    return self.get_query_set().get(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 334, in get
    num = len(clone)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 79, in __len__
    self._result_cache = list(self.iterator())
  File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 267, in iterator
    for row in compiler.results_iter():
  File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 674, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 719, in execute_sql
    sql, params = self.as_sql()
  File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 69, in as_sql
    where, w_params = self.query.where.as_sql(qn=qn, connection=self.connection)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/where.py", line 91, in as_sql
    sql, params = child.as_sql(qn=qn, connection=connection)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/where.py", line 94, in as_sql
    sql, params = self.make_atom(child, qn, connection)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/where.py", line 141, in make_atom
    lvalue, params = lvalue.process(lookup_type, params_or_value, connection)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/where.py", line 288, in process
    connection=connection, prepared=True)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/subclassing.py", line 53, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/subclassing.py", line 53, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/subclassing.py", line 53, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 350, in get_db_prep_lookup
    return [self.get_db_prep_value(value, connection=connection, prepared=prepared)]
  File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/subclassing.py", line 53, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/subclassing.py", line 53, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/subclassing.py", line 53, in inner
    return func(*args, **kwargs)
RuntimeError: maximum recursion depth exceeded in __instancecheck__

Then after it repeats hundreds of times it continues on over several files to the end of the traceback.

hexabits commented 14 years ago

Removing moderation.register(...) for the model in question, as well as the automoderate() call in my custom save_model method allowed syncdb to finish without error. I can't really tell the exact problem yet because I don't know enough about the source quite yet.

dominno commented 14 years ago

I could not reproduce your error, but i had other issue when loading objects from fixture, there was exception that ObjectDoesNotExist when checking for unchanged object in pre_save handler. I have fixed this, that objects that are loaded from fixtures does not create ModeratedObject instances for them. You can see it here 9047f6e6aff209c592fcceddf70d828fb504eabc

Please check if this also fix your issue. Also please ensure that you have latest source code.

hexabits commented 14 years ago

I actually have run manage syncdb a few times without getting that error, even before the latest commit.

Then I just realized that the error was not during manage syncdb but manage test and it was failing on when it tried importing fixtures, by looking in my shell history it was indeed happening during manage test.

hexabits commented 14 years ago

I do not receive these errors any longer when running manage test

dominno commented 14 years ago

I have managed to reproduce the error. I appears that it ocures when moderated object is saved but it should not be automoderated, automoderation causes multiple save call.

    profile = UserProfile.objects.get(user__username='moderator')
    profile.description = 'Change description'
    profile.save()
    profile.moderated_object.changed_by = user
    profile.moderated_object.save()
    automoderate(profile, user)

I will add is_automoderate parameter into save method of ModeeratedObject model, default false, it will it only set to True in automoderate helper.

This should fix the problem.

dominno commented 14 years ago

Or maybe it will be better to move automoderation to separate method, instead of doing it in save method of ModeratedObject. What do you think ?

dominno commented 14 years ago

Fixed issue with multible model save when automoderate was used. Auto moderation in save method of ModeratedObject has been moved to seperate method. Closed by 647b7f653c7feb0b16b4ce04c988bdae2721727d