I need to combine two instances of a model, and the process I need to go through is fairly simple. There's no much if any normalized data in the system. To combine the two I would just need to merge the data, and set the relevant foreign_keys on any models related.
The issue comes in trying to keep the app as 'reusable' as possible. I don't want to edit this module any time I add another model which might work with this one. is there a way to get a list of models with a foreign key to another specific model? and what the relevant fields are?
Also, a number of links back to this root model would be through generic foreign keys. Hoever I'm assuming finding the models linking to the root object would work on finding models linking to the ContentTypes model.
You might be able to pull this off via introspection, but you are tightly coupling your models to the Django implementation, which could be dangerous. If you are good with that, you could do something along the lines of:
class MyModel(models.Model): ## Definitions @property def related(self): related =  for attr in dir(self): try: ## Get the class name. attr_type = type(bf.__getattribute__(attr)).__name__ ## This is an internal type tucked away in: ## django/db/models/fields/related.py ## As best I can tell, this is only used for reverse ## relationships. if attr_type == "RelatedManager": related.extend(bf.__getattribute__(attr).all()) except: ## Some of the built in methods / properties throw errors, ## skip them. pass return related
Might not be the best solution, but it seems to work in testing.