我想验证表单的一个字段。这里是我的模型:如何通过忽略必填字段来验证特定表单的字段?
class Person(models.Model):
user = models.ForeignKey(User)
avatar = models.ImageField(upload_to=get_upload_path)
creation_time = models.DateTimeField(auto_now=True)
在我看来,我想让用户更新他们的头像。所以当用户点击更新按钮时,我只想发送头像文件并仅对其进行验证。
我update_avatar观点是在这里:
@csrf_exempt
def update_avatar(request,user_id):
profile_owner = Person.objects.get(user__id = user_id)
form = ProfileSettings()
if request.method == 'POST':
form = ProfileSettings(request.POST,request.FILES)
if form.is_valid():
return HttpResponse("ok")
return HttpResponse("else")
#the code is not complete, this is for only testing purpose =)
当我打电话form.is_valid(),我认为它看起来这需要其他信息了。所以你有一个想法,我如何才能验证只有头像部分?
你可以在你的模型上设置'required = False'属性吗? – Glycerine
我无法将它们设置为False,因为第一次创建用户配置文件时需要这些字段,但是在虚拟形象更新中,这些字段不是必需的 – iva123