Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/diegolozadev/DataMed/llms.txt

Use this file to discover all available pages before exploring further.

Overview

DataMed uses django-import-export to provide robust data export capabilities through the Django admin interface. This allows you to export patient data, cycle information, and clinical records in multiple formats.
Data export functionality is available through the Django Admin panel at /admin/. You must have admin or staff permissions to access these features.

Supported Export Formats

The import-export library supports multiple formats:

Excel (.xlsx)

Recommended for data analysis and reporting

CSV

Compatible with most data tools and databases

JSON

Machine-readable format for API integration

YAML

Human-readable structured data format

TSV

Tab-separated values for text processing

HTML

Browser-viewable tables

Accessing Export Features

1

Access Django Admin

Navigate to /admin/ and log in with administrator credentials
2

Select Model to Export

Choose the data type:
  • Patients (apps.patients.Patient)
  • Cycles/Ingresos (apps.patients.Ingreso)
  • Clinical Exams (various models in apps.exams)
3

Click Export Button

Look for the Export button at the top of the admin list view
4

Select Format

Choose your desired export format (Excel, CSV, etc.)
5

Download File

The file will be generated and downloaded automatically

Exporting Patient Data

Patient Admin Configuration

The Patient model is configured with import-export capabilities:
# From apps/patients/admin.py:8
from import_export.admin import ImportExportModelAdmin

@admin.register(Patient)
class PatientAdmin(ImportExportModelAdmin):
    form = PatientForm
    list_display = ('nombre', 'apellido', 'documento')
    list_filter = ('programa', 'genero')
    search_fields = ('nombre', 'apellido', 'documento')

Available Patient Fields

When exporting patients, all model fields are included:
  • nombre
  • apellido
  • tipo_documento
  • documento
  • fecha_nacimiento
  • genero
  • departamento
  • ciudad
  • zona
  • estado_civil
  • telefono
  • celular
  • entidad_salud
  • estrato
  • peso
  • altura
  • perimetro_abdominal
  • cuello
  • medico_remitente
  • especialidad
  • diagnostico_clinico
  • programa
  • valor_capita
The computed properties edad, ingreso_activo, esta_activo, and mes_capita are NOT included in exports by default as they are calculated fields, not database columns.

Filtering Before Export

You can filter data before exporting:
  1. Use the admin list filters (programa, genero)
  2. Use the search box (nombre, apellido, documento)
  3. Apply filters, then click Export
  4. Only filtered records will be exported

Exporting Cycle Data (Ingresos)

Ingreso Resource Configuration

# From apps/patients/admin.py:16
from import_export import resources, fields
from import_export.widgets import ForeignKeyWidget

class IngresoResource(resources.ModelResource):
    # Links to patient by primary key
    paciente = fields.Field(
        column_name='paciente_id',
        attribute='paciente',
        widget=ForeignKeyWidget(Patient, 'pk')
    )

    class Meta:
        model = Ingreso
        fields = ('id', 'paciente', 'fecha_inicio', 'fecha_fin', 
                  'estado', 'motivo')
        import_id_fields = ['id']

@admin.register(Ingreso)
class IngresoAdmin(ImportExportModelAdmin):
    resource_class = IngresoResource
    list_display = ('paciente', 'fecha_inicio', 'fecha_fin', 
                    'estado', 'motivo', 'mes_capita')
    list_filter = ('estado',)
    search_fields = ('paciente__nombre', 'paciente__apellido', 'motivo')

Exported Cycle Fields

  • id: Cycle unique identifier
  • paciente_id: Patient primary key reference
  • fecha_inicio: Program start date
  • fecha_fin: Program end date (if terminated)
  • estado: ACTIVO, SUSPENDIDO, or TERMINADO
  • motivo: Reason for status change
Use the estado filter to export only active patients for billing reports, or only terminated cycles for historical analysis.

Exporting Clinical Exam Data

While clinical exam models can be exported through the admin, they may need to be configured first:

Example: Enabling Export for Monitoreo

# Add to apps/exams/admin.py
from import_export.admin import ImportExportModelAdmin
from .models import Monitoreo

@admin.register(Monitoreo)
class MonitoreoAdmin(ImportExportModelAdmin):
    list_display = ('ingreso', 'created_at', 'modo_ventilatorio', 
                    'hipopnea_residual')
    list_filter = ('modo_ventilatorio', 'created_at')
    search_fields = ('ingreso__paciente__nombre', 
                     'ingreso__paciente__apellido')

Available Clinical Exam Models

You can configure export for any of these models:
  • Monitoreo: CPAP/BiPAP monitoring data
  • Psicologia: Psychology assessment scores
  • Nutricion: Nutritional status records
  • Neumologia: Pulmonology consultations
  • PolisomnografiaBasal: Baseline sleep studies
  • PolisomnografiaTitulacion: Titration studies
  • EquipoMedico: Medical equipment assignments
  • SeguimientoAdaptacion: Adaptation follow-up notes
To enable export for exam models, you need to update apps/exams/admin.py and register each model with ImportExportModelAdmin.

Data Import Capabilities

The import-export library also supports importing data. Use extreme caution when importing, as it can modify or create database records.

Import Process

1

Prepare Import File

Create an Excel or CSV file with the correct column names matching the model fields
2

Access Import

In the Django admin, click the Import button
3

Upload File

Select your file and choose the format
4

Preview Changes

Review the preview showing what will be created/updated
5

Confirm Import

Click Confirm import only if the preview looks correct

Import Configuration for Ingresos

# The IngresoResource is configured for import
class IngresoResource(resources.ModelResource):
    paciente = fields.Field(
        column_name='paciente_id',
        attribute='paciente',
        widget=ForeignKeyWidget(Patient, 'pk')
    )

    class Meta:
        model = Ingreso
        fields = ('id', 'paciente', 'fecha_inicio', 'fecha_fin', 
                  'estado', 'motivo')
        import_id_fields = ['id']  # Use 'id' to match existing records
For imports, the paciente_id column should contain the patient’s primary key (integer ID), not their document number.

Custom Export Workflows

Generating Monthly Billing Reports

1

Filter Active Cycles

Go to /admin/patients/ingreso/Apply filter: estado = ACTIVO
2

Export to Excel

Click Export → Excel format
3

Post-Processing

Open in Excel and:
  • Add mes_capita calculation (if needed)
  • Calculate valor_capita totals
  • Generate billing summary

Generating Program Completion Reports

1

Filter Terminated Cycles

Apply filter: estado = TERMINADO
2

Add Date Range Filter

Filter by fecha_fin in admin (may require customization)
3

Export and Analyze

Export to Excel and analyze completion reasons in the motivo field

Exporting Clinical Data for Research

Ensure you have proper permissions and ethical approval before exporting clinical data for research purposes. Anonymize data as required.
1

Configure Exam Admin

Add ImportExportModelAdmin to relevant exam models
2

Filter by Date Range

Use created_at filters to select date range
3

Export to CSV

CSV format is ideal for statistical analysis software
4

Anonymize Data

Remove or hash identifying fields before analysis

Custom Export Fields

You can customize which fields are exported by creating custom Resource classes:
from import_export import resources

class CustomPatientResource(resources.ModelResource):
    class Meta:
        model = Patient
        # Export only specific fields
        fields = ('documento', 'nombre', 'apellido', 'programa', 
                  'fecha_nacimiento', 'genero')
        export_order = ('documento', 'nombre', 'apellido', 'programa')

@admin.register(Patient)
class PatientAdmin(ImportExportModelAdmin):
    resource_class = CustomPatientResource  # Use custom resource

Exporting Computed Fields

To include computed properties like edad or mes_capita in exports:
from import_export import resources, fields

class PatientResourceWithAge(resources.ModelResource):
    edad = fields.Field()
    
    class Meta:
        model = Patient
        fields = ('documento', 'nombre', 'apellido', 'edad')
    
    def dehydrate_edad(self, patient):
        return patient.edad  # Call the @property method

class IngresoResourceWithMonth(resources.ModelResource):
    mes_capita = fields.Field()
    
    class Meta:
        model = Ingreso
        fields = ('id', 'paciente', 'fecha_inicio', 'estado', 'mes_capita')
    
    def dehydrate_mes_capita(self, ingreso):
        return ingreso.mes_capita  # Call the @property method
Use the dehydrate_<fieldname> method pattern to include computed properties in exports.

Pagination and Performance

Large exports may take time to process. The admin list view uses pagination (10 items per page), but exports process all filtered records.
# From apps/patients/views.py:48
paginator = Paginator(patients, 10) 
page_number = request.GET.get('page')
page_obj = paginator.get_page(page_number)

Best Practices for Large Exports

  1. Filter First: Apply filters to reduce dataset size
  2. Use CSV: CSV format is faster than Excel for very large datasets
  3. Off-Peak Hours: Schedule large exports during low-traffic periods
  4. Chunked Processing: For massive datasets, consider exporting by date ranges

Security Considerations

CRITICAL: Export functionality contains sensitive health data. Implement proper access controls.

Access Control

# Ensure only staff users can access admin
from django.contrib.auth.decorators import login_required
from django.contrib.admin.views.decorators import staff_member_required

# Admin views automatically require staff permissions
@staff_member_required
def admin_view(request):
    # Only accessible to staff users
    pass

Data Protection Checklist

  • ✅ Restrict admin access to authorized personnel only
  • ✅ Use HTTPS for all admin traffic (configured in settings.py)
  • ✅ Enable Django audit logging for exports
  • ✅ Regularly review who has admin access
  • ✅ Never commit exported files to version control
  • ✅ Encrypt exported files containing PHI (Protected Health Information)
  • ✅ Follow organizational data retention policies

Advanced: API-Based Export

For programmatic data access, consider implementing REST API endpoints:
# Example: Custom export view
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
import csv

@login_required
def export_active_patients(request):
    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename="active_patients.csv"'
    
    writer = csv.writer(response)
    writer.writerow(['Documento', 'Nombre', 'Mes Capita', 'Programa'])
    
    patients = Patient.objects.filter(ingresos__estado='ACTIVO').distinct()
    
    for patient in patients:
        ingreso = patient.ingreso_activo
        writer.writerow([
            patient.documento,
            f"{patient.nombre} {patient.apellido}",
            ingreso.mes_capita if ingreso else 'N/A',
            patient.programa
        ])
    
    return response

Configuration Reference

Settings.py Configuration

# From config/settings.py:54
INSTALLED_APPS = [
    'import_export',  # Enables export functionality
    'apps.users',
    'apps.exams',
    'apps.dashboard',
    'apps.patients',
    'django.contrib.admin',
    # ...
]

Import-Export Options

Common configuration options:
class MyResource(resources.ModelResource):
    class Meta:
        model = MyModel
        skip_unchanged = True  # Skip unchanged records on import
        report_skipped = True  # Report skipped records
        import_id_fields = ['id']  # Fields to match existing records
        exclude = ('created_at', 'updated_at')  # Exclude these fields
        export_order = ('id', 'name', 'status')  # Custom field order

Troubleshooting

Problem: Cannot see Export button in adminSolutions:
  • Verify user has staff permissions
  • Check that model uses ImportExportModelAdmin
  • Confirm import_export is in INSTALLED_APPS
  • Clear browser cache and refresh
Problem: Export generates empty fileSolutions:
  • Check if filters are too restrictive
  • Verify database contains matching records
  • Review Resource class field configuration
  • Check for errors in dehydrate methods
Problem: Related fields not exporting correctlySolution: Use ForeignKeyWidget in Resource class:
from import_export.widgets import ForeignKeyWidget

related_field = fields.Field(
    attribute='related_model',
    widget=ForeignKeyWidget(RelatedModel, 'field_name')
)
Problem: Export times out or is very slowSolutions:
  • Apply filters to reduce dataset size
  • Use CSV instead of Excel format
  • Consider exporting in batches by date range
  • Optimize database queries with select_related/prefetch_related

Patient Registration

Learn about the patient data structure

Managing Cycles

Understand cycle data for reporting

Recording Exams

See what clinical data is available

django-import-export Docs

Official library documentation

Build docs developers (and LLMs) love