Using Django's Databrowse With Inspectdb

The Django databrowse application is an interesting piece of software that I've wanted to try out ever since its creator, Adrian Holovaty, demoed it in Vancouver last year. Its purpose is to allow you to navigate data, using a web interface, looking for trends and other interesting aspects.

In conjunction with Django's inspectdb command, which creates Django model definitions by inspecting the schema of an existing database, you can potentially use databrowse on data from any application.

One annoyance with databrowse is having to register each model with it. I created a shell concoction to generate register statements for each model created by inspectdb and append them to urls.py:

python manage.py inspectdb\
  | grep "^class"\
  | sed "s/^class //g"\
  | cut -d"(" -f1\
  | xargs -I {} echo 'databrowse.site.register({})'\
  >> urls.py

I attempted to use databrowse on a database full of Drupal data, however, and ran into a current limitation of Django. Django models can't employ primary keys composed of multiple columns. Drupal's database schema uses this kind of primary key repeatedly.

5 comments on Using Django's Databrowse With Inspectdb

  1. pyjamas (not verified)
    Wed, 2010-02-17 21:58

    I have been using Django for a while now and it is a very useful piece of software indeed. white pyjamas

  2. realexams.com (not verified)
    Sat, 2009-11-28 08:11

    Its a very useful and interesting stuff. Keep sharing.

  3. Anonymous (not verified)
    Sat, 2009-11-14 05:03

    If your collation is not set to this in MySql?, it will report the wrong size. In my case, my table is configured to be 'latin1'. Changing the charset to 'latin1' in base.py caused inspectdb to report the correct length. However, that's obviously not a general solution. It would be best to make this caller-configurable (or better yet detected and altered when pulling the description off of the cursor object).

    life experience degree | Online Associate nursing degrees | life experience bachelor degree

  4. mike99 (not verified)
    Wed, 2009-10-28 12:28

    By contrast, Django has you define a model class — a subclass of django.db.models.Model — which explicitly declares all of its relevant fields via specialized subclasses of Field, including relational information. Coupled with any custom methods you want to define, and optional metadata inside an inner Meta class, this means that all the information about your model is encapsulated explicitly in the model class. Generally, Python has a preference for being explicit in this fashion, so it’s a good thing, but sometimes it’s nice to have the flexibility of a model that’s mostly 646-223 “auto-generated” from your database. which generates output you can save to a file and then edit and — once you’re happy with it — use as your live models. If you’ve got access to add new tables to the database, you can also add some of Django’s bundled applications (like the authentication and admin apps) to your INSTALLED_APPS, and hit the ground running. 1z0-051 This means that it’s fairly easy to take a database from an existing a et me start by talking about the language Django is written in. Django is a Python Framework, not a content management system, so you will be using Python to build Django web sites. That's a good thing, and these are some of the reasons why.
    Python is a dynamic language that is easy to read and work with. Including when you're working with other people's code. HP0-J25 Python is written in a procedural style, and has classes and objects when you need them, so it will be familiar to C++ and Java programmers. owever, it has dynamic types means you write less code and it's much faster to develop in than C++, Java. Also, It's a scripting language so there's no compiler to wait for.

    People run it everywhere from embedded systems to huge server clusters.

  5. Anonymous (not verified)
    Sat, 2008-10-04 16:08

    It's a bit OT but you can write that shell script a bit better:

    python manage.py inspectdb |
    grep "^class" |
    sed "s/^class //g" |
    cut -d"(" -f1 |
    xargs -I {} echo 'databrowse.site.register({})'\
    >> urls.py

    E.g., why use both a \ and a | when the | alone suffices?
    And of course you can go better: the following will
    combine the sed and grep steps:

    sed -n "/^class /s///gp" |

    With more work you could combine the grep, sed and cut with a single sed or (preferably) awk.