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

DocumentAdmin: 'StringField' object has no attribute 'document_type' #4

Closed audreyfeldroy closed 12 years ago

audreyfeldroy commented 12 years ago

I was getting the error "'StringField' object has no attribute 'document_type'"

Commenting out lines 341-361 of options.py resulted in getting the admin to work (although probably not the best thing to do, I'm guessing).

jschrewe commented 12 years ago

Could you post the document that produces the error? My guess is that there is an error in the first if in get_inline_instances.

justincy commented 12 years ago

I'm getting a similar error, except mine days "NoneType' object has no attribute 'document_type'". Perhaps bad data is causing this.

My fix was to change the if statement at line 346:

            if hasattr(f, 'field') and f.field:
                embedded_document = f.field.document_type
            elif hasattr(f, 'document_type'):
                embedded_document = f.document_type
            else:
                continue
jschrewe commented 12 years ago

Could you give me the definition of the document that causes this error? Because I don't see how and why an EmbeddedDocumentField can be None. But it may well be I'm overlooking something here. And while your fix certainly works, I guess it could lead to missing InlineAdmins for some documents.

justincy commented 12 years ago

What's the easiest way to view a the contents of a document? Printing just tells me what class it is.

jschrewe commented 12 years ago

print obj.__dict__ prints out all the attributes of the object. Mongoengine keeps all the data in the obj.data (or something similar) dict. None is usually assigned to data that is defined on the document schema, but not stored in the database.

justincy commented 12 years ago
{'_data': {'motherID': None, 'families': [], 'tags': [], 'personal': None, 'fatherID': None, 'childrenIDs': [], 'missing': None, 'api_id': None, 'events': None, 'note': None, 'cached': False, 'api': None, 'relation': None, 'user': None, 'spouseID': None, 'synced': datetime.datetime(2012, 2, 15, 11, 47, 49, 316099), 'parents': [], 'ordinances': None, 'id': None}, '_initialised': True}
jschrewe commented 12 years ago

Thanks for the data. Sadly it helps little without the schema. My attempts at recreating the error have so far failed miserably.

justincy commented 12 years ago
class Person(Document):
    user = ReferenceField('User',required=True)
    # valid api values are "nfs"
    api = StringField(max_length=50,required=True)
    api_id = StringField(max_length=10,required=True)
    cached = BooleanField(default=False)
    synced = DateTimeField(default=datetime.datetime.now)
    personal = EmbeddedDocumentField(Personal)
    events = EmbeddedDocumentField(Events)
    ordinances = EmbeddedDocumentField(Ordinances)
    families = ListField(EmbeddedDocumentField(Family))
    parents = ListField(EmbeddedDocumentField(Parents))
    missing = EmbeddedDocumentField(Missing)
    relation = StringField()
    motherID = StringField(max_length=10)
    fatherID = StringField(max_length=10)
    # spouse and children should eventually be kept as part of a family
    spouseID = StringField(max_length=10)
    childrenIDs = ListField()
    tags = ListField()
    note = StringField()
justincy commented 12 years ago

I found more documents that cause the problem:

'NoneType' object has no attribute 'document_type'
{'admin_site': <mongoadmin.sites.AdminSite object at 0x2770c50>, 'model': <class 'family.models.Person'>, 'document': <class 'family.models.Person'>, 'inline_instances': [<mongoadmin.options.EmbeddedStackedDocumentAdmin object at 0x2e945d0>], 'opts': <mongodbforms.documentoptions.DocumentMetaWrapper object at 0x2e944d0>}

'NoneType' object has no attribute 'document_type'
{'admin_site': <mongoadmin.sites.AdminSite object at 0x2770c50>, 'model': <class 'family.models.Person'>, 'document': <class 'family.models.Person'>, 'inline_instances': [<mongoadmin.options.EmbeddedStackedDocumentAdmin object at 0x2e945d0>, <mongoadmin.options.EmbeddedStackedDocumentAdmin object at 0x2e947d0>], 'opts': <mongodbforms.documentoptions.DocumentMetaWrapper object at 0x2e944d0>}

Are these documents that the admin module uses?

jschrewe commented 12 years ago

@justincy Mongoadmin does not have any documents. Your data appears to be the admin object itself.

I've added your fix in the last commit, so that should close that.