0

Consider the following scenario:

Our Django database objects must rely on IDs that are provided by external service A (ESA) - this is because we use this ID to pull the information about objects that aren't created yet from the external directly. ESA might shut down soon, so we also pull information about the same objects from external service B (ESB), and save them as a fallback.

Because these IDs are relied on heavily in views and URLs, the ideal scenario would be to use a @property:

@property
dynamic_id = ESA_id

And then, if ESA shuts down, we can switch easily by changing dynamic_id to ESB_id. The problem with this though, is that properties cannot be used in queryset filters and various other scenarios, which is also a must in this case.

My current thought is to just save ESA_id, ESB_id, and dynamic_ID as regular fields separately and assign dynamic_ID = ESA_id, and then, in case ESA shuts down, simply go over the objects and do dynamic_ID = ESB_id.

But I feel there must be a better way?

zerohedge
  • 3,185
  • 4
  • 28
  • 63

1 Answers1

1

Having ESA_id and ESB_id fields in the same table is a good solution, then you have some kind of setting (DEFAULT_SERVICE_ID='ESA_id'|'ESB_id') and your code change the lookup based on this option.

Here you can see an aproach to create filters dynamicly https://stackoverflow.com/a/310785/1448667

Xavi
  • 96
  • 3
  • Thanks. I do have them both in the database. The question is how to refer to them dynamically - meaning, if ESA went down, how can I switch to ESB with changing the references in the views. – zerohedge Sep 09 '18 at 09:01
  • 1
    The second part of my answwer was meant to give you a solution, wich I failed to do ;) If you change the querysets of your views to have the reference to the id you are using at the momtent it will do the trick. – Xavi Sep 09 '18 at 09:27