Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building object models around external data

Tags:

python

django

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.

like image 880
Zulan Avatar asked Sep 18 '20 12:09

Zulan


People also ask

What is an object based data model?

In object based data models, the focus is on how data is represented. The data is divided into multiple entities each of which have some defining characteristics. Moreover, these data entities are connected with each other through some relationships.

What is external uniqueness in Object-Role Modeling?

External uniqueness constraints in Object-Role Modeling span more than one role and where each role is in a different Fact Type. They are particularly useful to provide a preferred reference scheme for entities/entity types that have a compound/composite key when reduced to a relational model, or as similar for a graph database.

What is functional modeling in Ood?

Functional Modeling indicates the processes executed in an object and how data changes when it moves to objects. In OOD, a system is a group of objects. It involves designing the objects, classes, and relationships between classes. Terms in Object-Oriented Design:

What are the different types of data models?

Object based Data Models 1 Entity Relationship Data Model 2 Object Oriented Data Model 3 Semantic Data Model 4 Functional Data Model


2 Answers

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()

like image 145
Olzhas Arystanov Avatar answered Oct 24 '22 00:10

Olzhas Arystanov


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.

like image 45
Alexey Popov Avatar answered Oct 24 '22 00:10

Alexey Popov