David542
David542

Reputation: 110203

Is there a DESCRIBE function for Django Models?

I want to know the columns in a table. Is there a DESCRIBE table; in django?

Upvotes: 8

Views: 3581

Answers (4)

Udi
Udi

Reputation: 30502

You can also browse your models' fields and documentation through the admindocs app ( https://docs.djangoproject.com/en/dev/ref/contrib/admin/admindocs/ ) :

Django’s admindocs app pulls documentation from the docstrings of models, views, template tags, and template filters for any app in INSTALLED_APPS and makes that documentation available from the Django admin.

Upvotes: 1

David542
David542

Reputation: 110203

The easiest way I found is to use print Class.__doc__, for example:

>>> from network.models import Network
>>> print Network.__doc__
Network(id, network, name, location)

Upvotes: 6

Brian Fisher
Brian Fisher

Reputation: 23989

You can use model._meta.get_all_field_names() to get a list of all the fields in a model class.

Upvotes: 9

Zach Kelling
Zach Kelling

Reputation: 53819

You can use ./manage.py sqlall <app name> to print the SQL used to create the table for a given app. For the tutorial project's polls app the output is:

tutorial% ./manage.py sqlall polls
BEGIN;
CREATE TABLE "polls_poll" (
    "id" integer NOT NULL PRIMARY KEY,
    "question" varchar(200) NOT NULL,
    "pub_date" datetime NOT NULL
)
;
CREATE TABLE "polls_choice" (
    "id" integer NOT NULL PRIMARY KEY,
    "poll_id" integer NOT NULL REFERENCES "polls_poll" ("id"),
    "choice" varchar(200) NOT NULL,
    "votes" integer NOT NULL
)
;
CREATE INDEX "polls_choice_763e883" ON "polls_choice" ("poll_id");
COMMIT;

Which you might find useful.

Upvotes: 6

Related Questions