16👍
✅
If you want to make the field read-only during creation you should do it the other way round:
def get_readonly_fields(self, request, obj=None):
if obj is None:
return ['headline']
return []
85👍
There is no need to override get_readonly_fields
. Simplest solution would be:
class ItemAdmin(admin.ModelAdmin):
exclude=("headline ",)
readonly_fields=('headline', )
When using readonly_fields
you can’t override get_readonly_fields
, because default implementation reads readonly_fields variable. So overriding it only if you have to have some logic on deciding which field should be read-only at time.
- [Django]-How to filter (or replace) unicode characters that would take more than 3 bytes in UTF-8?
- [Django]-Django: order_by multiple fields
- [Django]-Django urls straight to html template
11👍
Just do editable=False, while adding the field in the model
headline = models.CharField(max_length=255,editable=False)
It wont be editable and you cannot see the field from admin panel, but you can add values to them from a method within the model
- [Django]-Fetching static files failed with 404 in nginx
- [Django]-Multiple ModelAdmins/views for same model in Django admin
- [Django]-Nginx.service: Failed to read PID from file /run/nginx.pid: Invalid argument
5👍
Other way is:
def get_form(self, request, obj=None, **kwargs):
form = super().get_form(request, obj=None, **kwargs)
if obj is None:
form.base_fields["my_field"].disabled = True
return form
- [Django]-Best way to integrate SqlAlchemy into a Django project
- [Django]-How to put timedelta in django model?
- [Django]-How can I tell the Django ORM to reverse the order of query results?
Source:stackexchange.com