Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set the fields `null=True`, why in the ModelForm I still get required error?

Tags:

python

django

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?

like image 747
user7693832 Avatar asked Oct 14 '25 15:10

user7693832


2 Answers

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 and TextField. If a string-based field has null=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.

like image 138
ikkuh Avatar answered Oct 17 '25 04:10

ikkuh


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

like image 45
Nakul Narayanan Avatar answered Oct 17 '25 05:10

Nakul Narayanan