Model.Form.save 后未获取 ForeignKey 值

Not getting ForeignKey value after Model.Form.save

我的 table Project

中有以下属性
sales_person = models.ForeignKey(MyUser, related_name='ad_sales_person', editable=False,null=True, blank=True)

我有一个像这样的模型表格。

class ProjectForm(forms.ModelForm):
    class Meta:
        model = Project
 exclude = ('created_by', 'current_user', 'project_current_state')


 def save(self, request=None, commit=False):
        project = super(ProjectForm, self).save(commit=commit)
        project.sales_person_id = project.customer.created_by.id
        project.save()
        project.sales_person # I should get the User object here. But i am not getting it here? 

我想在保存模型对象后获取 sales_person 对象。我没有做错什么?

不,*_id 字段的赋值不会加载相应的模型实例。所以你应该手动加载实例:

project.save()
project.sales_person = MyUser.objects.get(pk=project.sales_person_id)

但是为什么不为该字段分配一个 MyUser 实例呢?这不会增加任何性能损失:

project.sales_person = project.customer.created_by