1

I'm writing a simple scaffolding app in django, however I'm still not able to access a models fields and attributes(e.g. CharField, max_length=100, null=True, etc...). I know about the _meta class of models, but as far as I know it only retrieves basic info about the model not the fields. Is there a away to achieve this?

Update: you can find the answer in this article: http://www.b-list.org/weblog/2007/nov/04/working-models/

user3474181
  • 93
  • 2
  • 7
  • possible duplicate of [Get model's fields in Django](http://stackoverflow.com/questions/3647805/get-models-fields-in-django) – Serafeim Sep 28 '14 at 20:11

1 Answers1

2

You should use the get_field method to get info for a particular field:

field = ModelName._meta.get_field('field_name')

Then you may check various attributes of field, like field.blank, field.null, field.name etc.

If on the other hand you want to get a list of all fields of a Model you should use fields:

fields = ModelName._meta.fields

For example to get the name of all your model fields you can do something like:

field_names = ', '.join(f.name for f in fields)

Hm... also I just noticed that your question is a duplicate of Get model's fields in Django !

Community
  • 1
  • 1
Serafeim
  • 14,962
  • 14
  • 91
  • 133