Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

215
Vistas
Building object models around external data

I want to integrate external data into a Django app. Let's say, for example, I want to work with GitHub issues as if they were formulated as normal models within Django. So underneath these objects, I use the GitHub API to retrieve and store data.

In particular, I also want to be able to reference the GitHub issues from models but not the other way around. I.e., I don't intend to modify or extend the external data directly.

The views would use this abstraction to fetch data, but also to follow the references from "normal objects" to properties of the external data. Simple joins would also be nice to have, but clearly there would be limitations.

Are there any examples of how to achieve this in an idiomatic way?

Ideally, this would be would also be split in a general part that describes the API in general, and a descriptive part of the classes similar to how normal ORM classes are described.

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

I would suggest to just use normal OOP principles, Polymorphism, Association etc. to get a similar feel to real models.

But I'm not sure I would try to simulate behavior as close as I could, because the ORM is specifically designed for database interaction. I would just write my custom methods.

over 4 years ago · Santiago Trujillo Denunciar

0

The django way in this case would be to write a custom "db" backend.

This repo looks abandoned but still can lead you to some ideas.

over 4 years ago · Santiago Trujillo Denunciar

0

If you want to use Django Model-like interface for your Github Issues, why don't use real Django models? You can, for example, create a method fetch in your model, that will load data from the remote api and save it to your model. That way you won't need to make external requests everywhere in your code, but only when you need it. A minimal example will look like these:

import requests
from django.db import models

from .exceptions import GithubAPIError


class GithubRepo(models.Model):
    api_url = models.URLField()  # e.g. https://api.github.com/repos/octocat/Hello-World


class GithubIssue(models.Model):
    issue_id = models.IntegerField()
    repo = models.ForeignKey(GithubRepo, on_delete=models.CASCADE)

    node_id = models.CharField(max_length=100)
    title = models.CharField(max_length=255, null=True, blank=True)
    body = models.TextField(null=True, blank=True)

    """
    Other fields
    """

    class Meta:
        unique_together = [["issue_id", "repo"]]

    @property
    def url(self):
        return f"{self.repo.api_url}/issues/{self.issue_id}"

    def fetch_data(self):
        response = requests.get(self.url)
        if response.status != 200:
            raise GithubAPIError("Something went wrong")

        data = response.json()

        # populate fields from repsonse
        self.title = data['title']
        self.body = data['body']

    def save(
        self, force_insert=False, force_update=False, using=None, update_fields=None
    ):
        if self.pk is None:  # fetch on first created
            self.fetch_data()
            super(GithubIssue, self).save(
                force_insert, force_update, using, update_fields
            )


You can also write a custom Manager for your model that will fetch data every time you call a create method - GithubIssue.objects.create()

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda