aircraft
aircraft

Reputation: 26886

Can we fill the fields I don't need in Serializer fields?

I have a User Model, and it has many fields in it:

class User(models.Model):

    openstackcloud_id = models.CharField(max_length=32, null=True)  # 
    username = models.CharField(max_length=16)
    password = models.CharField(max_length=40)  # sha1加密
    real_name = models.CharField(max_length=12, null=True,blank=True)
    phone = models.CharField( max_length=11)  # 手机号码
    email = models.EmailField(blank=True, null=True )
    qq = models.CharField(max_length=10, null=True, blank=True)
    address = models.CharField(max_length=64, blank=True, null=True)  # 地址
    id_card = models.CharField(blank=True, null=True, max_length=18, validators=[RegexValidator(regex='^.{18}$', message='身份证长度必须为18', code='nomatch')])
    id_card_img_front = models.CharField(max_length=256, blank=True, null=True)
    id_card_img_back = models.CharField(max_length=256, blank=True, null=True)
    nickname = models.CharField(max_length=16, blank=True, null=True)
    profile = models.CharField(max_length=256, blank=True, null=True, default='我爱旗云')  # 个人简介
    usertype = models.ForeignKey(to='UserType', default=1, blank=True)  # 默认是:1.普通用户

    user_c_type = models.CharField(max_length=4, null=True)  # 用户类型(个人 or 企业)
    fixed_phone = models.CharField(max_length=16, null=True)  # 固定电话
    fax = models.CharField(max_length=16, null=True)  # 传真
    main_bussiness = models.CharField(max_length=16, null=True)  # 主营业务
    main_industry = models.CharField(max_length=16, null=True) # 所属行业
    company_name = models.CharField(max_length=32, null=True)  # 公司名称
    company_address = models.CharField(max_length=32, null=True)  # 公司地址
    province = models.CharField(max_length=32, null=True, default="--省--")  # 省市县
    town = models.CharField(max_length=32, null=True, default="--市--")  # 省市县
    country_level = models.CharField(max_length=32, null=True, default="--县--")  # 省市县

    ctime = models.DateTimeField(auto_now_add=True)
    uptime = models.DateTimeField(auto_now=True)   # 更新时间
    status = models.CharField(max_length=1, null=True, default=1) # 1.存活  2.禁用 3.注销

You see, there is many fields in User model.

But I am using rest framework, like:

class UserSerializer(serializers.ModelSerializer):

    class Meta:
        model = User
        fields = ('username', 'password', .....)

I should write all of the user information I need in my UserSerializer fields, obviously its a fussy thing.

So, is there a way to fill the field I don't need? If can, it is convenience for us.

Upvotes: 2

Views: 37

Answers (1)

Aneesh R S
Aneesh R S

Reputation: 3827

yes you can. use exclude. Include all fields by using '__all__' and remove the unwanted field using exclude.

For example, if you want to remove 'ctime' and 'uptime' but need to include all other fields, then

class UserSerializer(serializers.ModelSerializer):

    class Meta:
        model = User
        fields = '__all__'
        exclude = ('ctime', 'uptime')

Upvotes: 3

Related Questions