jschrewe / django-mongoadmin

Integrates mongodb into django's admin
http://www.schafproductions.com/projects/mongo-admin/
BSD 3-Clause "New" or "Revised" License
112 stars 38 forks source link

Maximum recursion exceeded with a ReferenceField('self') in Document #20

Closed Karmak23 closed 11 years ago

Karmak23 commented 11 years ago

Hi,

We encounter a crash with the following code:

in models:

class Tag(Document):
    name     = StringField(verbose_name=_(u'name'), unique=True)
    slug     = StringField(verbose_name=_(u'slug'))
    language = StringField(verbose_name=_(u'language'), default='')
    parents  = ListField(ReferenceField('self'), default=list)
    children = ListField(ReferenceField('self'), default=list)
    origin   = GenericReferenceField(verbose_name=_(u'Origin'),
                                     help_text=_(u'Initial origin from where '
                                     u'the tag was created from, to eventually '
                                     u'help defining other attributes.'))
    duplicate_of = ReferenceField('Tag', verbose_name=_(u'Duplicate of'),
                                  help_text=_(u'Put a "master" tag here to '
                                  u'help avoiding too much different tags '
                                  u'(eg. singular and plurals) with the same '
                                  u'meaning and loss of information.'))

    meta = {
        'indexes': ['name', ]
    }

in admin.py:

class TagAdmin(admin.DocumentAdmin):

    list_display = ('id', 'name', 'language', 'duplicate_of', )
    list_display_links = ('id', 'name', )
    search_fields = ('name', 'slug', 'duplicate_of', )
    change_list_filter_template = "admin/filter_listing.html"

admin.site.register(Tag, TagAdmin)

Stacktrace of the crash. If I change the referencefield of the duplicate_of attribute to 'self' instead of 'Tag', I get another stacktrace crash, but I think it's basically the same problem.

It doesn't occur if I use ListField(ReferenceField(… [same content]…)), but obviously I do not want to use a list.

Any hint on this one ?

regards,

jschrewe commented 11 years ago

You can either use the newest github version of mongodbforms or fix it yourself by changing the last if statement in _setup_document_fields in documentoptions.py to:

if isinstance(f, ReferenceField) and not isinstance(f.document_type._meta, DocumentMetaWrapper) \
        and self.document != f.document_type:
    f.document_type._meta = DocumentMetaWrapper(f.document_type)

A new version of mongodbforms is coming, but there is one more bug that needs fixing and some thought before.