make django model field read only or disable in admin while saving the object first time
Asked Answered
S

4

29

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?

Susurrate answered 2/2, 2015 at 10:10 Comment(3)
Why are you not using readonly_fields property instead?Rosenfeld
it will work for editing the objects not for creating the object first time.Susurrate
It will work for both if you are not overwriting get_readonly_fields.Rosenfeld
S
16

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 []
Strongwilled answered 2/2, 2015 at 10:28 Comment(3)
it worked thanks a lot. But its not showing the input box disabled.Susurrate
hay its not working for edit!!. While editing its not disabled.Susurrate
Sorry I misunderstood the question, thought you only want to disable it for creating... otherwise just always return ['headline']... No need for the If-clause...Strongwilled
R
86

There is no need to override get_readonly_fields. Simplest solution would be:

class ItemAdmin(admin.ModelAdmin):
    exclude=("headline ",)
    readonly_fields=('headline', )

When using readonly_fields you can't override get_readonly_fields, because default implementation reads readonly_fields variable. So overriding it only if you have to have some logic on deciding which field should be read-only at time.

Rosenfeld answered 2/2, 2015 at 15:58 Comment(3)
I have no idea why people are not using your simple suggestion hereAnent
This is the way to go!Eldoraeldorado
great solution!Kolnick
S
16

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 []
Strongwilled answered 2/2, 2015 at 10:28 Comment(3)
it worked thanks a lot. But its not showing the input box disabled.Susurrate
hay its not working for edit!!. While editing its not disabled.Susurrate
Sorry I misunderstood the question, thought you only want to disable it for creating... otherwise just always return ['headline']... No need for the If-clause...Strongwilled
H
12

Just do editable=False, while adding the field in the model

headline = models.CharField(max_length=255,editable=False)

It wont be editable and you cannot see the field from admin panel, but you can add values to them from a method within the model

Hinduism answered 23/5, 2021 at 8:2 Comment(0)
C
5

Other way is:

    def get_form(self, request, obj=None, **kwargs):
        form = super().get_form(request, obj=None, **kwargs)

        if obj is None:

            form.base_fields["my_field"].disabled = True

        return form
Colossae answered 5/12, 2020 at 22:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.