Accessing Values of a ForeignKey or OneToOne object in ModelForm
I have a ModelFormA
fo开发者_如何学运维r ModelA
, which has a one-one relationship with ModelB
and foreign-key relationship with ModelC
.
Inside ModelFormA
, I can access attributes of the current ModelA instance via self.cleaned_data["colA-1"]
. How would I access attributes of ModelB or ModelC?
If you want the current values of the object, rather than the values submitted by the form, you actually need to use self.instance
. You can then do self.instance.myforeignkey
or whatever you need to follow relationships.
self.cleaned_data["foreign_key_column"] actually returns an instance of the related model. Same goes for one-to-one relationship
精彩评论