I get required
error when I request the API by me.
In my models.py
:
class User(models.Model):
username = models.CharField(max_length=16)
password = models.CharField(max_length=16)
real_name = models.CharField(max_length=12, null=True)
phone = models.CharField( max_length=11)
email = models.EmailField(null=True)
qq = models.CharField(max_length=10, null=True)
address = models.CharField(max_length=64, null=True)
id_card = models.CharField(null=True, max_length=18, validators=[RegexValidator(regex='^.{18}$', message='id card length:18', code='nomatch')])
id_card_img_front = models.CharField(max_length=256, null=True)
id_card_img_back = models.CharField(max_length=256, null=True)
nickname = models.CharField(max_length=16, null=True)
profile = models.CharField(max_length=256, null=True, default='my profile')
usertype = models.ForeignKey(to='UserType', default=1)
ctime = models.DateTimeField(auto_now_add=True)
uptime = models.DateTimeField(auto_now=True)
In my views.py
, please pay attention I only send username
, password
, phone
to the request:
class UserMF(ModelForm):
class Meta:
model = models.User
fields = '__all__'
def register(request):
...
obj = UserMF(request.POST)
if obj.is_valid():
...
else:
print (obj.errors.as_json()) # then printed the errors
...
The error message:
{"qq": [{"message": "This field is required.", "code": "required"}], "profile": [{"message": "This field is required.", "code": "required"}], "id_card_img_front": [{"message": "This field is required.", "code": "required"}], "id_card": [{"message": "This field is required.", "code": "required"}], "real_name": [{"message": "This field is required.", "code": "required"}], "usertype": [{"message": "This field is required.", "code": "required"}], "id_card_img_back": [{"message": "This field is required.", "code": "required"}], "address": [{"message": "This field is required.", "code": "required"}], "nickname": [{"message": "This field is required.", "code": "required"}], "email": [{"message": "This field is required.", "code": "required"}]}
In my models I have set the the fields (except username, password, phone) to null=True
, why in the ModelForm I still get required error?
With both null=True
and blank=True
on a CharField
you will remove this required checking, but it's better to remove the null=True
. Django's advice:
Avoid using null on string-based fields such as
CharField
andTextField
. If a string-based field hasnull=True
, that means it has two possible values for “no data”:NULL
, and the empty string.
As you seem to be using only CharField
with null=True
I would change all of those to blank=True
.
You can use "fields" option in the meta class of the model form, you can specify what all fields are required as a set like
fields = ('username', 'password', 'phone')
or you can use "exclude" option in the model form meta class to remove unwanted fields like exclude ("excluded_field1","excluded_field2") etc
you can find the description here modelform
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With