Suppose I have the following database structure:
class Asset(models.Model):
# ...
class AbstractBuilding(models.Model):
asset = models.OneToOneField(Asset, on_delete=models.CASCADE, primary_key=True)
# ...
class Meta:
abstract = True
class Office(AbstractBuilding):
# ...
class Lab(AbstractBuilding):
# ...
class PowerPlant(AbstractBuilding):
# ...
If I have an Office
object, it's easy to get the corresponding Asset
through the one-to-one field (e.g. office_object.asset
returns an Asset
). However, suppose I have a Asset
instead. How can I get the corresponding Office
(or Lab
or PowerPlant
) from the Asset
object?