Skip to main content

GTFS Django

PyPI version Python versions Django versions License

A Django package that provides abstract models for GTFS (General Transit Feed Specification) data, covering both static Schedule feeds and dynamic Realtime feed messages.

Because all models are abstract, this package creates no database tables of its own. You subclass the models in your own Django app, giving you full control over your schema — adding foreign keys, extra fields, constraints, or any other customisations your project requires.

The package gtfs-django also includes custom Django model fields that handle GTFS-specific data types and formats, such as time fields that support hours greater than 24, geographic coordinate fields, and validated fields for color codes, language tags, and more.

Finally, gtfs-django provides utilities for importing GTFS data from ZIP files, parsing GTFS Realtime protobuf messages, and validating GTFS feed contents against the specification.

Requirements

  • Python: 3.12+
  • Django: 5.2+

Installation

pip install gtfs-django
uv add gtfs-django

Quick Start

1. Add to your Django settings

INSTALLED_APPS = [
    # ...
    "gtfs",
]

2. Subclass the abstract models in your app

Add auxiliary tables or new fields as needed.

from django.db import models
from gtfs.models import BaseAgency, BaseRoute, BaseTrip, BaseStop, BaseStopTime


class Feed(models.Model):
    """Represents a versioned GTFS feed download."""
    name = models.CharField(max_length=255)
    downloaded_at = models.DateTimeField(auto_now_add=True)


class Agency(BaseAgency):
    feed = models.ForeignKey(Feed, on_delete=models.CASCADE)


class Route(BaseRoute):
    feed = models.ForeignKey(Feed, on_delete=models.CASCADE)
    agency = models.ForeignKey(Agency, on_delete=models.CASCADE)


class Trip(BaseTrip):
    feed = models.ForeignKey(Feed, on_delete=models.CASCADE)


class Stop(BaseStop):
    feed = models.ForeignKey(Feed, on_delete=models.CASCADE)


class StopTime(BaseStopTime):
    feed = models.ForeignKey(Feed, on_delete=models.CASCADE)

3. Run migrations for your app

python manage.py makemigrations <your_app_name>
python manage.py migrate

Abstract Models

GTFS Schedule

Model GTFS file Description
BaseAgency agency.txt Transit agencies
BaseStop stops.txt Stops and stations
BaseRoute routes.txt Routes
BaseTrip trips.txt Trips
BaseStopTime stop_times.txt Arrival/departure times per stop
BaseCalendar calendar.txt Weekly service schedules
BaseCalendarDate calendar_dates.txt Service exceptions
BaseShape shapes.txt Route shapes
BaseTimeframe timeframes.txt Fare timeframes
BaseRiderCategory rider_categories.txt Fare rider categories
BaseFareMedia fare_media.txt Fare media
BaseFareProduct fare_products.txt Fare products
BaseFareLegRule fare_leg_rules.txt Fare leg rules
BaseFareLegJoinRule fare_leg_join_rules.txt Fare leg join rules
BaseFareTransferRule fare_transfer_rules.txt Fare transfer rules
BaseArea areas.txt Areas
BaseStopArea stop_areas.txt Stop-to-area assignments
BaseNetwork networks.txt Route networks
BaseRouteNetwork route_networks.txt Route-to-network assignments
BaseFrequency frequencies.txt Frequency-based service
BaseTransfer transfers.txt Transfers between stops
BasePathway pathways.txt Station pathways
BaseLevel levels.txt Station levels
BaseLocationGroup location_groups.txt On-demand location groups
BaseLocationGroupStop location_group_stops.txt Group-to-stop assignments
BaseLocation locations.geojson On-demand service areas
BaseBookingRule booking_rules.txt On-demand booking rules
BaseTranslation translations.txt Field translations
BaseFareAttribute fare_attributes.txt Fare definitions
BaseFareRule fare_rules.txt Fare applicability rules
BaseFeedInfo feed_info.txt Feed metadata
BaseAttribution attributions.txt Attribution information

GTFS Realtime

Model Description
BaseFeedMessage Feed message header
BaseTripUpdate Real-time trip schedule updates
BaseStopTimeUpdate Per-stop arrival/departure updates
BaseVehiclePosition Live vehicle locations
BaseAlert Service alerts and disruptions

Custom Fields

gtfs.fields provides Django model fields that encode GTFS type semantics:

Field GTFS type Notes
GTFSColorField Color 6-digit hex, without #
GTFSCurrencyCodeField Currency code ISO 4217, e.g. USD
GTFSCurrencyAmountField Currency amount Decimal
GTFSDateField Date Accepts/serialises YYYYMMDD
GTFSTimeField Time timedelta; supports hours ≥ 24
GTFSLocalTimeField Local time String HH:MM:SS; hours 0-23
GTFSIDField ID CharField, optionally ASCII-only
GTFSTextField Text CharField with 255-char default
GTFSTimezoneField Timezone Validated IANA timezone name
GTFSLanguageCodeField Language code BCP 47 tag, e.g. en-US
GTFSLatitudeField Latitude Decimal, -90 to 90
GTFSLongitudeField Longitude Decimal, -180 to 180
GTFSPhoneNumberField Phone number Permissive international format
GTFSEnumField Enum Integer with required choices
GTFSNonNegativeIntegerField Non-negative integer Integer ≥ 0
GTFSNonZeroIntegerField Non-zero integer Integer other than 0
GTFSPositiveIntegerField Positive integer Integer > 0
GTFSNonNegativeFloatField Non-negative float Decimal ≥ 0
GTFSPositiveFloatField Positive float Decimal > 0

Development

git clone https://github.com/simovilab/gtfs-django.git
cd gtfs-django

# Install with development dependencies
uv sync

# Run tests
pytest

Regenerating Schedule models

gtfs/builder/schedule.yaml is the source of truth for the GTFS Schedule models. After changing it or gtfs/builder/mappings.py, generate gtfs/builder/models.py from the repository root:

uv run python -m gtfs.builder.builder

The generated file contains Schedule models only. Review and migrate its contents into gtfs/models.py manually; the generator does not modify that file.

Contributing

Contributions are welcome. For major changes please open an issue first.

  1. Follow Django coding standards
  2. Add tests for new features
  3. Update documentation as needed
  4. Ensure compatibility with Django 5.2+

License

Apache License 2.0 — see LICENSE for details.

Credits

Developed by SIMOVI Lab.


For more information about GTFS, visit gtfs.org.

Metadata

Release files for gtfs-django 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gtfs-django 0.3.0
File Size Uploaded Provenance Actions
gtfs_django-0.3.0.tar.gz 141.6 kB Without provenance Details for gtfs_django-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for gtfs-django 0.3.0
File Interpreter ABI Platform Actions
gtfs_django-0.3.0-py3-none-any.whl Python 3 none any Details for gtfs_django-0.3.0-py3-none-any.whl

Total release size: 300.7 kB

Release files / gtfs_django-0.3.0.tar.gz

Download URL gtfs_django-0.3.0.tar.gz
Size 141.6 kB
Tags Source
SHA-256 checksum
How to use checksums
36e53050a130acd7f03f1ae7abeb41eed335524abfe68f07fed026e1d453bf21
BLAKE2b-256 checksum
How to use checksums
2dd596ef24262e0e838a7cc1db62a73125ab0ea7f7552a4799e5e46c7c369639
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.4

Release files / gtfs_django-0.3.0-py3-none-any.whl

Download URL gtfs_django-0.3.0-py3-none-any.whl
Size 159.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dcc100e0af3d943d4beed390dac104ce540a9941c8ce1e09fe1c902b2519e037
BLAKE2b-256 checksum
How to use checksums
ceeb910c356d21a04e91692aac5b74d7327dc9b886483066799a41d04daad0c3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.4

Security for gtfs-django 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page