Django admin: make field editable in add but not edit

后端 未结 3 429
南笙
南笙 2020-12-24 11:19

I\'ve got a model similar to this:

class Product(models.Model):
    third_party_id = models.CharField(max_length=64, blank=False, unique=True)
相关标签:
3条回答
  • 2020-12-24 11:51

    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 []
    
    0 讨论(0)
  • 2020-12-24 12:11

    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,]
    
    0 讨论(0)
  • 2020-12-24 12:14

    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!

    0 讨论(0)
提交回复
热议问题