我有一个简单的Package模型
from models import Package from django import forms class PackageForm(forms.ModelForm): class Meta: model= Package fields= ['name', 'version', 'url', 'description', 'arch', 'dependancies', 'conflicts', 'file']如果文件扩展名(类是FileField)是否为.sh,我如何要求modelform在验证中检查?
有没有办法把它放在模型中? 我只能在视图中管理它吗?
谢谢
编辑:另外,忘了问,模型有auth用户模型的外键...它将包含当前用户..模型如何管理?
再次感谢
感谢你的回答! 我抓住了这个......虽然我遇到了问题
包含django.contrib.auth.models的外键用户模型,处理表单时如何告诉modelform将当前用户对象传递给模型实例? 我想到了这个......
views.py
def add(request): if request.method == 'POST': the_model= PackageForm(request.user, request.POST, request.FILES) if the_model.is_valid(): the_model.save()我在模型中覆盖了init :
from models import Package from django import forms class PackageForm(forms.ModelForm): def __init__(self,user,*args,**kwargs): super (PackageForm,self ).__init__(*args,**kwargs) # populates the post self.fields['maintainer_name'].queryset = user # adds the user object passed by add in views.py class Meta: model= Package fields= ['name', 'version', 'url', 'description', 'arch', 'dependancies', 'conflicts', 'file']manteiner_name是ForeignKey(用户)对象...它给了我一个keyerror :(这不好......任何解决方案?
谢谢!
And i have a simple modelform for Package
from models import Package from django import forms class PackageForm(forms.ModelForm): class Meta: model= Package fields= ['name', 'version', 'url', 'description', 'arch', 'dependancies', 'conflicts', 'file']How can i ask the modelform to check, within validation, if the file extension (class is FileField) is .sh for example?
is there a way to put this in modelform? of can i only manage it in a view?
Thanks
Edit: Also, forgot to ask, the model has a Foreignkey to the auth User model... which is going to contain the current user.. how can modelform manage that?
Thanks again
Thanks for the answer! i'm getting hold of this.. although i encounter a problem
Package contains a foreignkey to django.contrib.auth.models User model, When the form is processed how can i tell the modelform to pass the current user object to the model instance? i thought of this...
views.py
def add(request): if request.method == 'POST': the_model= PackageForm(request.user, request.POST, request.FILES) if the_model.is_valid(): the_model.save()i overwrited the init in modelform:
from models import Package from django import forms class PackageForm(forms.ModelForm): def __init__(self,user,*args,**kwargs): super (PackageForm,self ).__init__(*args,**kwargs) # populates the post self.fields['maintainer_name'].queryset = user # adds the user object passed by add in views.py class Meta: model= Package fields= ['name', 'version', 'url', 'description', 'arch', 'dependancies', 'conflicts', 'file']manteiner_name is the ForeignKey(User) object... it gives me a keyerror :( that's not good... Any solutions?
Thanks!
最满意答案
您应该阅读有关进行exttra验证的django documentatino: http : clean_file()您只需要定义一个clean_file()方法。
You should read the django documentatino on doing exttra validation: http://docs.djangoproject.com/en/1.1/ref/forms/validation/#ref-forms-validation You just need to define a clean_file() method.
更多推荐
发布评论