13

I'd like to use peewee to create records from a csv. It looks like the syntax requires keyword args:

user = User.create(username='admin', password='test')

If the rows in the csv look like (admin, test), it would be convenient to know the field names ('username', 'password') to form a dict to pass in (it looks like I can't pass in a list of the values for the model to infer what the corresponding fields are).

Is there a property of User that has a list of the field names in the order that they are defined?

Thanks

beardc
  • 20,283
  • 17
  • 76
  • 94

1 Answers1

19

Looks like it's User._meta.get_field_names()

I just saw someone else mention it in another question.

Community
  • 1
  • 1
beardc
  • 20,283
  • 17
  • 76
  • 94
  • Note to self: don't use `User` as a model name – beardc Mar 14 '14 at 18:57
  • I just stumbled on this question: Why wouldn't you use User as a model name? – tfeldmann May 28 '14 at 12:10
  • 2
    It's a [reserved word in postgres](http://stackoverflow.com/questions/10891368/postgres-table-column-name-restrictions). Peewee handles it fine, but it makes it trickier to use the console. – beardc May 28 '14 at 15:35
  • 23
    Update for those that stumble onto this question: For newer versions of peewee, you need to use `User._meta.sorted_fields` or `User._meta.sorted_field_names` – booshong Jan 29 '16 at 01:47
  • 2
    I had to use `._meta.fields.keys()` because `sorted_field_names` wasn't returning Foreign Keys (sometimes). – kev Sep 23 '16 at 02:33
  • @kev -- that's not correct. Were you expecting it to return back-references or something? – coleifer Jan 11 '18 at 16:55
  • @Ian thank, couldn't find in in peewee document. how did you find it? – TomSawyer Apr 11 '19 at 14:30