I want to disable the few fields from model in django admin while saving initially.
"<input type="text" id="disabledTextInput" class="form-control" placeholder="Disabled input">"
like this.
My model is:
class Blogmodel(models.Model):
tag = models.ForeignKey(Tag)
headline = models.CharField(max_length=255)
image=models.ImageField(upload_to=get_photo_storage_path, null=True, blank=False)
body_text = models.TextField()
pub_date = models.DateField()
authors = models.ForeignKey(Author)
n_comments = models.IntegerField()
i want to disable the "headline" and "n_comments". i tried it in admin.py file, but its not disabling the fields on initial saving. But for editing the fields its working, it making the fields read only.
in admin.py
class ItemAdmin(admin.ModelAdmin):
exclude=("headline ",)
def get_readonly_fields(self, request, obj=None):
if obj:
return ['headline']
else:
return []
Headling getting disabled but for edit only. i want to disable it at the time of object creation. i.e. first save. can anyone guide me for this?
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 []