Question

I am building a wrapper around an API, and as such I want to hold information that the API itself holds (I want any updated api information to be held on my server as well). This being the case, I need to build models that anticipate the data I will be receiving.

So I started building these models. Let us assume they look like this:

class ModelForAPI(models.model):
    #model fields here

Now this data is going to steadily be collected and updated accordingly when it is needed to. Let's then assume that in addition to the models ive built to hold the api data, I have more data that is related but that is relevant on my server only. The question becomes: how is the best way to "extend" this data?

I feel that there a few possible options, either I extend the api model like such:

class Model(models.Model):
    api_information = ForeignKey(ModelForAPI)

Or I subclass it like such:

class Model(ModelForAPI):
    #model fields here

Since I am dealing with API information that is going to be updated frequently with new information, I am not sure which would make my life easier in terms of model architecture. Any pointers would be much appreciated.

Was it helpful?

Solution

One of the most simple ways to achieve this is to just store a serialized version of your api data on an api-by-api basis.

class ModelForAPI( models.model ):
  name = models.CharField( ... )
  data = models.CharField( max_len = SOMETHING_BIG )

def get_paypal_config():
  return json.loads( ModelForAPI.objects.get( name = 'PayPal' ).data )

def set_paypal_config( mydict )
  qs = ModelForAPI.objects.filter( name = 'PayPal' )
  qs.update( data = json.dumps( mydict ))
  return

In this way, you get a lot of flexibility in the actual API data without having to modify your schema. You can also store various API settings in the same model.

(Of course... it's often recommended that API variables be stored in environment variables a la Heroku, but that's your call.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top