简体   繁体   中英

how to set a None value with django form

i have a form with a google map and when the user does not click on the map i need to send latitude and longitude as None to views.py,this is views.py:

 if request.method == "POST":
    if request.user.is_authenticated:

        if  True:
            form = CreatMeetingForm(request.POST,request.FILES)
            print(request.POST)
            
            if form.is_valid():
                if 'id' in request.POST:
                   
                    obj= get_object_or_404(Meeting, id=request.POST.get('id'))
                    form = CreatMeetingForm(request.POST or None,request.FILES or None, instance= obj)
                    meeting= form.save(commit= False)
                    members = form.cleaned_data['members_email']
                   
                    try:
                        meeting.lat= round(form.cleaned_data['lat'],14)
                        meeting.lng = round(form.cleaned_data['lng'],14)
                    except :
                        meeting.lat= None
                        meeting.lng = None
                        
                    meeting.host = request.user
                    update = True
                    meeting.save()
                    
                else :
                    
                    meeting=form.save(commit=False)
                    members = form.cleaned_data['members_email']
                    try:
                        meeting.lat= round(form.cleaned_data['lat'],14)
                        meeting.lng = round(form.cleaned_data['lng'],14)
                    except :
                        meeting.lat= None
                        meeting.lng = None
                        
                    meeting.host = request.user
                    update=False
                    meeting.save()
            else:
                print('form not valid')
                form.errors["code"] = 0
                return JsonResponse(form.errors)
    else:
        return HttpResponse('Unauthorized', status=401)

and this is my model fields:

lat = models.DecimalField(max_digits=19, decimal_places=16,blank=True,null=True)
lng = models.DecimalField(max_digits=19, decimal_places=16,blank=True,null=True)

when i want to create a new form my code works but when in edit mode and i send the id in requet.POST it raises this error:

{"lat": ["Enter a number."], "lng": ["Enter a number."], "code": 0}

it seems i get the error in form.is_valid bcs i can see the print('form not valid') any suggestion?

It might help to add the default value to the field definitions like so:

lat = models.DecimalField(
    max_digits=19,
    decimal_places=16,
    blank=True,
    null=True,
    default=None
)
lng = models.DecimalField(
    max_digits=19,
    decimal_places=16,
    blank=True,
    null=True,
    default=None
)

Edit: If this doesn't help, please share the definition of CreatMeetingForm.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM