Django admin: make field editable in add but not edit

后端 未结 3 456
南笙
南笙 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 []
    

提交回复
热议问题