2

I want to exclude specific column in rest framework only for view data. (not for put data)

[models.py]

class article(models.Model):
    articleNo = models.AutoField(primary_key=True)
    userkey = models.CharField(max_length=50, null=False)
    content = models.CharField(max_length=500, null=False)
    password = models.CharField(max_length=20, null=False, default='1234')
    date = models.DateTimeField(auto_now_add=True)

[serializers.py]

class articleSerializer(serializers.ModelSerializer):
    class Meta:
        model = article
        fields = ('articleNo', 'userkey', 'content', 'password', 'date')

After I change fields = ('articleNo', 'content', 'date') from serializers.py,

Only articleNo, content, date will be show in rest framework.

But, When I PUT data to article, It doesn't inserted properly.

After insert data, MySQL data is below.

articleNo|content | date                       |userkey    |password    
    37   | Test   | 2018-03-13 08:01:07.424564 |           | 1234

userkey is blank and password is default value.

Maybe I have to modify serializers.py I think, But I don't know how to fix it.

How can I insert data properly?

1
  • PEP8 correct python variable name will be article_no and not articleNo Commented Mar 13, 2018 at 13:58

1 Answer 1

2

no you cant do that, if you remove any field, that means that field wont be taken while passing from serializer,

instead try this

class articleSerializer(serializers.ModelSerializer):
    userkey = serializers.CharField(write_only=True)
    password = serializers.CharField(write_only=True)
    class Meta:
        model = article
        fields = ('articleNo', 'userkey', 'content', 'password', 'date')
Sign up to request clarification or add additional context in comments.

4 Comments

It works exactly what I want! Thanks for quickly reply!!
glad could help bro :)
I'm new at Stackoverflow. Accept the answer is click the up arrow button side at your post?
Ok. I clicked it. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.