我目前有一个模型表单,它向数据库提交输入的域。在
我遇到的问题是,我需要保存当前登录的用户ID(来自django.auth公司表)提交域以满足db端的PK-FK关系时。在
我目前有:
class SubmitDomain(ModelForm):
domainNm = forms.CharField(initial=u'Enter your domain', label='')
FKtoClient = User.<something>
class Meta:
model = Tld #Create form based off Model for Tld
fields = ['domainNm']
def clean_domainNm(self):
cleanedDomainName = self.cleaned_data.get('domainNm')
if Tld.objects.filter(domainNm=cleanedDomainName).exists():
errorMsg = u"Sorry that domain is not available."
raise ValidationError(errorMsg)
else:
return cleanedDomainName
而且视图.py
^{pr2}$问题是给了我一个错误:(1048,“列'FKtoClient\uid'不能为空”),对于列FKtoClient
,它试图提交:7L
而不是{
如果有人能帮忙,我会非常感激的
您可以从请求对象获取登录用户:
如果要要求用户登录以提交表单,可以执行以下操作:
首先,从表单中删除
FKtoClient
。您需要在视图中设置用户,以便对请求对象执行“是”。不能在窗体上设置自动设置当前用户的属性。在实例化表单时,可以传递一个
tld
实例,该实例已经有用户集。在这比@dm03514的答案有一个优势,即如果需要,您可以在form方法中以}。在
self.instance.user
的形式访问{相关问题 更多 >
编程相关推荐