Answer the question
In order to leave comments, you need to log in
How to get field value from linked model?
The two classes are linked via ForeignKey:
#Class1:
field1 = models.ForeignKey('Class2', null=True, blank=True)
c = Class1.objects.get(id=1)
c.field1 #значение таблицы Class1 вместо объекта Class2
c._meta.get_field('field1').rel.to #Class2
Answer the question
In order to leave comments, you need to log in
The ForeignKey parameter passed the name of the model as a string, not its object. The documentation says that this is possible in the case for yet undefined models, but not in mine.
field1 = models.ForeignKey(Class2, null=True, blank=True)
c.field1 gives you an object, not a table value.
If you need the class itself and not the object, you can do type(c.field1)
Through a double underscore...
I.e. something likec.get_field('field1__поле_из_связной_таблицы')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question