【发布时间】:2012-11-19 12:24:59
【问题描述】:
我有一个模型,作者为ForeignKey,例如:
class Appointment(models.Model):
# ...
author = models.ForeignKey(User)
我希望在为当前登录的用户创建约会时自动设置author 字段。换句话说,作者字段不应该出现在我的 Form 类中:
class AppointmentCreateForm(ModelForm):
class Meta:
model = Appointment
exclude = ('author')
有两个问题:
- 如何在通用 CreateView 中访问表单并设置
author? - 如何告诉表单保存排除的字段以及从用户输入中读取的值?
【问题讨论】:
标签: django django-generic-views