Skip to main content

django-fields-iterator provides the FieldsIterator class that allow to iterate on some or all of the fields of a model

Project description

Purpose

The purpose of the django_fields_iterator package is to provide a convenient way to iterate on all fields of a model, or only a subset.

Example

It’s as easy as instantiating the FieldsIterator class with your model, and then iterate:

from django_fields_iterator import FieldsIterator

iterator = FieldsIterator(MyModel)

for field, name, field_type in iterator:
    do_stuff()

The field is the real Field object from django. The name is the attribute name to access this name from an instance the field type is one of FieldTypes object, as defined later in this document.

Excluding fields by name

If you don’t want some fields to be returned, you can do it by passing the ignore_fields to the FieldsIterator constructor:

iterator = FieldsIterator(MyModel, ignore_fields=['afield', 'anotherfield'])

Excluding fields by type

Some times you don’t want some fields, for example, to exclude the ManyToManyField:

from django_fields_iterator import FieldsIterator, FieldTypes

iterator = FieldsIterator(MyModel)

for field, name, field_type in iterator.iter_fields(exclude=[FieldTypes.M2M]):
    do_stuff()

The exclude argument is a list that can be empty, or a combination of the following:

  • FieldTypes.SIMPLE: For simple fields

  • FieldTypes.FK: For foreign keys (and one to one fields)

  • FieldTypes.M2M: For many to many fields

  • FieldTypes.REVERSE_FK: For reverse foreign keys, ie foreign keys defined on another model, pointing on the model we use

  • FieldTypes.REVERSE_M2M: For reverse many to many fields, ie many to many fields defined on another model, pointing on the model we use

  • FieldTypes.REVERSE_O2O: For reverse one to one fields, ie one to one fields defined on another model, pointing on the model we use

Including only some field types

The iter_fields method also accepts a only argument, accepting the same kind of value than the exclude argument. Both arguments cannot be set at the same time.

If for example you only want simple fields:

from django_fields_iterator import FieldsIterator, FieldTypes

iterator = FieldsIterator(MyModel)

for field, name, field_type in iterator.iter_fields(only=[FieldTypes.SIMPLE]):
    do_stuff()

Iterating fields for a specific type

The FieldsIterator class provides methods to iterate on fields of a certain type:

  • iter_simple_fields, for fields of type FieldTypes.SIMPLE

  • iter_foreign_keys, for fields of type FieldTypes.FK

  • iter_manytomany_fields, for fields of type FieldTypes.M2M

  • iter_reverse_foreign_keys, for fields of type FieldTypes.REVERSE_FK

  • iter_reverse_manytomany_fields, for fields of type FieldTypes.REVERSE_M2M

  • iter_reverse_onetoone_fields, for fields of type FieldTypes.REVERSE_O2O

Overriding

The FieldsIterator provides two methods that are great to override to avoid some fields to be returned, based on their class for example:

  • can_return_field: for fields directly defined on the model

  • can_return_reverse_field: for fields defined on another model that point to the current one

They simple accept the field (as returned by the get_fields django method on the model _meta attribute) and the field type (maybe useful in subclasses), and should return a boolean.

They simply check if the field name (the accessor name for the reverse fields) is in the ignore_fields attribute, but can be overridden to add more logic.

Installation

Install from PyPI:

pip install django-fields-iterator

Requirements

  • Python 3.9, 3.10, 3.11, 3.12

  • Django 4.2, 5.0, 5.1

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_fields_iterator-1.3.1.tar.gz (64.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_fields_iterator-1.3.1-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file django_fields_iterator-1.3.1.tar.gz.

File metadata

  • Download URL: django_fields_iterator-1.3.1.tar.gz
  • Upload date:
  • Size: 64.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_fields_iterator-1.3.1.tar.gz
Algorithm Hash digest
SHA256 75000979d71be6a4d18d5ecb5ca0f8b6dc410013319b8a36a182037a7d78ff2a
MD5 38a5f365565c95a2c0dcfa2da202974e
BLAKE2b-256 c01ca0fe72eb198492f0a3365a68c8e8d7ec6719ba981c187236f162f2ddc63f

See more details on using hashes here.

File details

Details for the file django_fields_iterator-1.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_fields_iterator-1.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 03504f859423d8b4e57813aa2a735193ec925c207dce640c1a2ed00c61afc85b
MD5 b12b774a2ea976b59faa8cb35c9d6acb
BLAKE2b-256 263fcbc993d99146354d3884ea2978b1833a7e1dbc199da9e7892e1c4aeaa61c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page