66
Do not set self.readonly_fields
to avoid thread issues. Instead override get_readonly_fields method:
def get_readonly_fields(self, request, obj=None):
if obj: # obj is not None, so this is an edit
return ['third_party_id',] # Return a list or tuple of readonly fields' names
else: # This is an addition
return []
5
The above is helpful (shanyu’s answer using get_readonly_fields), however it does not work properly if used in “StackedInline”. The result is two copies of whatever field is marked readonly, and it is not editable in the “add” instance. See this bug: https://code.djangoproject.com/ticket/15602
Hope this saves someone some searching!
- [Django]-Linking to the django admin site
- [Django]-UnicodeDecodeError: 'ascii' codec can't decode byte 0xd1 in position 2: ordinal not in range(128)
- [Django]-Get virtualenv's bin folder path from script
1
I am not sure if this is the best way, but you could define your own form for the admin. And custom validate your third_party_id, rejecting if it is already set:
Admin.py
class ProductAdminForm(forms.ModelForm):
class Meta:
model = Product
def clean_third_party_id(self):
cleaned_data = self.cleaned_data
third_party_id = cleaned_data['third_party_id']
id = cleaned_data['id']
obj = Product.objects.get(id=id)
if obj.third_party_id != third_party_id:
raise ValidationError("You cannot edit third_party_id, it must stay as %s" % obj.third_party_id)
return third_party_id
class ProductAdmin(admin.Admin):
form = [ProductAdminForm,]
- [Django]-How do I JSON serialize a Python dictionary?
- [Django]-How to iterate through dictionary in a dictionary in django template?
- [Django]-Django allauth example [Errno 61] Connection refused
Source:stackexchange.com