Making Fields readonly after an object has been created in django-admin

Model:

class Command(models.Model):
        command = models.TextField(('Command(s)'))
        description = models.TextField(blank=True, null=True)
        os = models.ForeignKey(OperatingSystem)

        def __unicode__(self):
           return self.command

Admin:

class CommandAdmin(admin.ModelAdmin):
        list_display= ['command','description','os']
        readonly_fields=[]
        fields =['command','description','os']

        def get_readonly_fields(self, request, obj=None):
           if obj is None:
              '''obj has not been created yet'''
              return self.readonly_fields
           else:
              '''we already know about this object make command readonly'''
              return self.readonly_fields + ['command']

Tagged as admin , django django-admin , readonly
Written by Andrew Konkol on July 18th, 2012

0 Comments

Log in with Twitter, Google, Facebook, LinkedIn to leave a comment.