I have this model:
from django.db import models
class TestField(models.Model):
M = 'Male'
F = 'Female'
O = 'Other'
GENDER = [
(M, 'Male'),
(F, 'Female'),
(O, 'Other'),
]
field = models.CharField(max_length=50, choices=GENDER)
class Meta:
db_table = 'test_fields'
def __str__(self):
return self.field
and I have this view:
from rest_framework.views import APIView
from rest_framework.response import Response
from testback.models import TestField
class TestBack(APIView):
def post(self, request):
gender = request.data.get('gender')
if TestField.objects.create(field=gender):
return Response("Success")
else:
return Response("Failure")
the form on the front end sends the data to the back and the data I send is FormData() that looks like this:
HTML
<form action="" method="post">
<fieldset>
<label for="editGender">Gender</label>
<select id="editGender" v-model="gender.name">
<option selected disabled value="">Select</option>
<option>Female</option>
<option>Male</option>
<option>Other</option>
</select>
</fieldset>
<button type="button" @click="sendData">Send</button>
</form>
JAVASCRIPT
import { ref } from "vue";
import { store } from "@/store";
import axios from "axios";
const gender = ref({
name:"",
});
function sendData() {
var formData = new FormData();
formData.append("gender", gender.value.name);
axios.post("/api/v1/test/", formData).then((response) => {
store.messages.push(response);
});
}
When I click the button I get to save the model in the database and if I choose any option from the list that option is saved in the database correctly
BUT if I manually edit the form using dev tools and i change the options to anything even Javascript tags then the model gets created but the field is empty.
Based on the model should the model not be prevented from being created if the options is not one of the choices? Doesn’t this pose a security risk if someone can just change the options in dev tools and save the field to the database? What is the point then of having options if I can just have a normal charfield and do my own validation since there is no validation on the field and choices really serve no purpose?
Validation on the front end can be manipulated so validation on the back end is vital but this seems to not work at all.