I have multiple Django models that reference a model using foreign keys as such:
class LocationHistory(models.Model):
region = models.ForeignKey(WorldGrid)
...
class UserSynthVals(models.Model):
region = models.ForeignKey(WorldGrid)
...
class RegionalVictoryRate(models.Model):
region = models.ForeignKey(WorldGrid)
...
Where WorldGrid is just:
class WorldGrid(models.Model):
latitude = models.FloatField()
longitude = models.FloatField()
...
Now, I am able to get all the models that reference WorldGrid (abstractly for reusability) with:
models_that_reference = [i.model for i in get_model('appname',model_name)._meta.get_all_related_objects()]
And at that point I can loop through them getting their model.objects.all().values(), but I cannot find a way to join those separate lists of objects into one that I could then output to a table.
I would rather do something more along the lines of django’s expected use (like what select_related() does) and keep this abstract.
Any help solving this problem would be appreciated, or a new direction to try.
I was able to find a pretty good way of doing this, and the select_related was indeed the key. Once I have a list of the referencing models, I can do:
To get all of the fields that I am trying to see at once, and then:
To get them all into one giant list. If your models are historic (with datetime fields), or have circular foreignkeys, you will probably need to prune this result a little more before displaying it, but this gets everything together nicely.