0

I'm fairly new to django web development. And I got an error whereby I try to change a 'post' under admin url - so localhost:8080/admin. I'm able to create it successfully but when I try to click the post that I had just added. I'm getting this error:

Exception Type: DatabaseError Exception Value: This query is not supported by the database.

And this is the code that I know is 'messing' with this query:

#Post is an abstract class
class BlogPost(Post):
      ...
      translators = models.ManyToManyField(Staff, related_name='translators')
      photographers = models.ManyToManyField(Staff, related_name='photographers')  
      authors = models.ManyToManyField(Staff, related_name='authors')
      ...

To explain what is going on with this blog post - it can have multiple 'owners'/people that contributed to this post and thus the decision using ManyToManyField. And vice-versa with the 'Staff' member - the type of 'member' can have multiple ownership on multiple posts (Let me know if this logic doesn't make any sense because it does to me).

I'm using mongodb for the database, django 1.5.11 and I have installed djangotoolbox. I've tried the following solutions with adding a relationship to BlogPost as shown below:

Class Staff(Member):
    ...
    staff_posts = models.ManyToManyField(BlogPost, related_name="staff_posts")
    ...

But I'm getting an error on 'cannot import BlogPost'. I tried figuring out the reason of this error and I don't think that I have a circular dependance - after checking all of the files, there's no circular dependance.

macmania314
  • 457
  • 2
  • 18
  • 1
    You should realize that mongodb is not a standard relational database. django-nonrel tries to hide this as much as possible, but not all queries will work. Is there any reason you are using it, rather than a standard SQL db like PostgreSQL/MySQL/sqlite? – Daniel Roseman Jul 02 '15 at 12:05
  • I'm fairly new to web development and I wasn't sure what the schema structure would be for this project and based on what I read - mongo is good for 'getting started' and my colleagues didn't have a lot of problems with the mongodb. – macmania314 Jul 02 '15 at 12:09
  • 2
    don't use Mongo unless you have a very good specific reason for doing so... Django works best with a relational db, and for most use cases PostgreSQL will work better than Mongo anyway. you can handle your 'evolving' schema using [migrations](https://docs.djangoproject.com/en/1.8/topics/migrations/) – Anentropic Jul 02 '15 at 12:13
  • I was wondering - will using SQL databases able to handle 'heavy analytics'. I'm hoping further along with this project - I can build a dashboard that will analyze the statistics of blog-posts over-time in different regions. It's just that I heard mongo is good for data-analytics. I have limited experience with using databases and my experience has been using SQL which works fairly well with the project. I will keep your recommendation in mind though. :) – macmania314 Jul 02 '15 at 12:15
  • 1
    also what @Chris Hawkes said... you don't need the `staff_posts` field on the `Staff` model... Django automatically provides the [reverse lookups](https://docs.djangoproject.com/en/1.8/topics/db/queries/#lookups-that-span-relationships) for the relations defined on `BlogPost` model – Anentropic Jul 02 '15 at 12:16
  • the question you should ask is whether Mongo can handle 'heavy analytics'. The last job I worked at doing heavy analytics we used [Amazon Redshift](http://aws.amazon.com/redshift/) ...it is specialised for this type of work but has a Postgres-compatible API and you can connect from Django via the usual `postgresql_psycopg2` backend – Anentropic Jul 02 '15 at 12:19
  • I have to second or third the opinion not to use MongoDB. Relational databases are a much better option. If MySQL can scale to Facebook level than why bother with Mongo. Plus MySQL, Postgresql and SQLServer (2016) support raw JSON storage. – Chris Hawkes Jul 02 '15 at 12:46
  • Thanks for the help, dropped mongo as the database and now everything works :) This was definitely a good lesson in picking which database to use. Thanks again everyone – macmania314 Jul 03 '15 at 14:39

2 Answers2

0

Just to put it out there, I'm not real familiar with MongoDB.

However, I don't believe you need to define a ManyToManyField on your Staff class. You already have a ManyToMany defined in your BlogPost, having it defined in one class file is all that is required. (At least for MySQL).

Chris Hawkes
  • 11,923
  • 6
  • 58
  • 68
  • Hey Chris, I removed that code under "Staff". Still giving me that pesky Database exception error. – macmania314 Jul 02 '15 at 12:00
  • so staff does not point to your BlogPost anymore? if that is the case, you may want to try rebuilding the database if you can easily drop it, restart the server etc... – Chris Hawkes Jul 02 '15 at 12:03
  • yup - did the manage.py flush command to reset the database & restarted the server. – macmania314 Jul 02 '15 at 12:04
  • sorry, i threw it out there I'm not familiar with MongoDB. I am pretty sure about what I'm saying in regards to MySQL. Like the previous guy mentioned it appears manytomany is not something that just works using MongoDB. I found this project to address it, but I have never used it. https://github.com/huandzh/django-mongom2m – Chris Hawkes Jul 02 '15 at 12:05
0

MongoDB (or mongoengine, which I'm guessing you're using) doesn't support joins, so the typical way to model many-to-many relations in a relational database has to be implemented some other way.

One way is to use a ReferenceField inside a ListField. It might look like this (not tested):

class BlogPost(Post):
    authors = models.ListField(models.ReferenceField(Staff))
    ...

Also see these answers:

Community
  • 1
  • 1
André Laszlo
  • 15,169
  • 3
  • 63
  • 81
  • 1
    Hey Andre - tried the path you suggested but gave up and decided to use postgresql. Thanks for the prompt response – macmania314 Jul 03 '15 at 14:40