Opened 9 years ago

Last modified 9 years ago

#26081 closed Uncategorized

Understanding MyModel._meta.get_fields() — at Version 1

Reported by: Thomas Güttler Owned by: nobody
Component: Database layer (models, ORM) Version: 1.9
Severity: Normal Keywords:
Cc: tzanke@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Thomas Güttler)

We try to understand what MyModel._meta.get_fields() does.

https://docs.djangoproject.com/en/1.9/ref/models/meta/#django.db.models.options.Options.get_fields

 Returns a tuple of fields ...

get_fields() returns two different types of objects:

  • django.db.models.fields
  • ManyToOneRel, OneToOneRel or ManyToManyRel

If you print the items of get_fields() you see the difference:

<ManyToOneRel: foo.tickettype>
<ManyToOneRel: foo.ticket>
...
foo.Tickettype.id
foo.Tickettype.parent
foo.Tickettype.name
...

According to the docs you get "fields". But you get first Rel-Objects and then DB-fields.

The example in the docs is the same: https://docs.djangoproject.com/en/1.9/ref/models/meta/#django.db.models.options.Options.get_fields

(<ManyToOneRel: admin.logentry>,
 <django.db.models.fields.AutoField: id>,
...

We are missing a common base class.

The wording is confusing since you speak of "field" but get_fields() returns fields and rels.

Change History (1)

comment:1 by Thomas Güttler, 9 years ago

Component: DocumentationDatabase layer (models, ORM)
Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top