django-nonrel / djangotoolbox

Django tools for building nonrel backends
BSD 3-Clause "New" or "Revised" License
200 stars 125 forks source link

This database doesn't support filtering on non-primary key ForeignKey fields. #76

Open jeffsgraham opened 7 years ago

jeffsgraham commented 7 years ago

I apologize if this is the wrong place to post this, but I thought I would mention it in case others were having the same trouble I was. Also, so I can get feedback if I am missing something or otherwise committing a heinous sin.

My project using django-nonrel and djangotoolbox 1.8.0 with a MongoDB 2.6 data store raises an exception "This database doesn't support filtering on non-primary key ForeignKey fields." when I try to (surprise) filter on non-primary FK fields.

It appears to be a true statement, I cannot find any documentation regarding MongoDB supporting relations between collections on anything other than a primary key. However, just because it isn't supported doesn't mean it doesn't work. In fact it works exactly as I expected it to. Removing the conditional that raises this exception in lines 233-234 of basecompiler.py seems to result in proper filtering behavior from MongoDB. This saved me from writing custom forms for all my admin models.

Disclaimer: I have only tested this in the context of my current application, more testing is recommended. Likely the statement in question is there for a reason, a better solution than removing it entirely is likely necessary.

I have attached a patch file detailing the changes I made. djangotoolbox-basecompiler-1.8.mongo.patch.txt