How to make field readonly for update.

26 views
Skip to first unread message

Андрей Казанцев

unread,
May 5, 2020, 5:49:25 AM5/5/20
to Django REST framework
I have a model with fields: A, B, C. I want all fields on create be available, but on update only A filed to be avaliable. How to do this?

shaik mahammad gouse

unread,
May 5, 2020, 7:23:22 AM5/5/20
to django-res...@googlegroups.com
Hi,

Can you please explain more about the issue..

Thank you 
Shaik Mahammad gouse

On Tue, May 5, 2020 at 3:19 PM Андрей Казанцев <hec...@yandex.ru> wrote:
I have a model with fields: A, B, C. I want all fields on create be available, but on update only A filed to be avaliable. How to do this?

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/d3a6b838-ae44-4be1-8fcc-dfefb1a1506e%40googlegroups.com.

zhong ming wu

unread,
May 5, 2020, 11:33:41 AM5/5/20
to django-res...@googlegroups.com
1.  You can override `.save` method of a model and detect that it is an update operation like this
```
def save(self, *args, **kwargs): 
 if not self._state.adding:
```
According to https://docs.djangoproject.com/en/3.0/ref/models/instances/  one can supply `update_fields` to the save method and then you can raise
        exception when that parameter is not to your liking during the update.

2. If you rely on DRF serializer, you can create a different serializer for updating

3. Since this is a forum on DRF, to prevent update via http, you write different views for PUT vs POST ie you allow insert into all fields in your POST view but  updates on your whitelisted fields in PUT view. 


On May 5, 2020, at 05:49, Андрей Казанцев <hec...@yandex.ru> wrote:

I have a model with fields: A, B, C. I want all fields on create be available, but on update only A filed to be avaliable. How to do this?

Reply all
Reply to author
Forward
0 new messages