验证Django ModelForm中的数据(Validating data in Django ModelForm)

编程入门 行业动态 更新时间:2024-10-09 20:22:52
验证Django ModelForm中的数据(Validating data in Django ModelForm)

我有一个简单的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.

更多推荐

本文发布于:2023-08-02 21:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1381363.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   ModelForm   Django   Validating   data

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!