Reputation: 2033
I'm puzzled with this probably easy problem.
My Models:
class DiseaseLibrary(models.Model):
name = models.CharField(max_length=255)
subadults = models.BooleanField(default=False,blank=True)
adults = models.BooleanField(default=False, blank=True)
def __str__(self):
return self.name
class BoneChangeBoneProxy(models.Model):
anomalies = models.ForeignKey('DiseaseLibrary', on_delete=models.CASCADE, related_name='anomalies')
technic = models.ForeignKey('Technic', on_delete=models.CASCADE, related_name='technic_proxy')
bone_change = models.ForeignKey('BoneChange', blank=True, null=True, on_delete=models.CASCADE, related_name='bone_change_proxy')
bone = TreeManyToManyField('Bone', related_name='bone_proxy')
From DiseaseLibrary I'd like to get all Objects that link to it via related_name "anomalies". Namely "technic_proxy", "bone_change_proxy", "bone_proxy" which are ForeignKeys to other models.
I would expect to get access by the related name "anomalies" and _set
>>> ds = DiseaseLibrary.objects.all().first()
>>> ds.name
'Some nice name'
>>> ds.anomalies
<django.db.models.fields.related_descriptors.create_reverse_many_to_one_manager.<locals>.RelatedManager object at 0x107fa4f10>
>>> ds.anomalies_set.all()
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'DiseaseLibrary' object has no attribute 'anomalies_set'
>>>
How can I access all ForeignKey values in model BoneChangeBoneProxy through model DiseaseLibrary?
Upvotes: 1
Views: 118
Reputation: 476624
Since you have set the related_name='anomalies'
, you query with:
# ↓ related_name of the ForeignKey
ds.anomalies.all()
Upvotes: 2