Reputation: 2287
Suppose I have two models as such
class first(models.Model):
name = models.CharField(max_length=20)
y = models.ForeignKey(second)
class second(models.Model):
random = models.CharField(max_length=20)
Now if I want value of name
for a first
object instance using getattr
, I can do getattr(x, 'name')
where x
is a first
object but if I try the same with getattr(x, 'y.random')
, it throws me an error even though x.y.random
is a totally valid query.
Is it possible to query x.y.random
if all I have is object x
and string 'y.random'?
Upvotes: 1
Views: 1503
Reputation: 30453
That's because it tries to fetch property 'y.random'
, though there is no such property, and it's not even valid to have a property with .
.
getattr(getattr(x, 'y'), 'random'))
Upvotes: 2