1

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.

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

This is exactly the type of thing I was looking for, do you know anywhere I can read up on this type of thing? It seems complicated given your example (mainly because im not familiar with this concept :p)
Not sure of any reference material (specifically). In essence, this is just a way to store basic python objects (eg. a dictionary) in the database as a string. Used sparingly, eg. with config variables, this is probably fine. You wouldn't want to do that for "normal" models though, since it makes it difficult to query any of the data fields using the ORM.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.