Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a DESCRIBE function for Django Models?

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

like image 415
David542 Avatar asked May 14 '11 03:05

David542


2 Answers

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

like image 200
Brian Fisher Avatar answered Oct 22 '22 09:10

Brian Fisher


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.

like image 36
zeekay Avatar answered Oct 22 '22 11:10

zeekay