Home > Enterprise >  Django Admin - Show form json input value as text insted string
Django Admin - Show form json input value as text insted string

Time:12-30

Considering I have a model like:

MyStore = (
id = 1,
name = 'Foobar',
information_as_json = {
    'open_at': datetime.now(),
    'close_at': datetime.now()   timedelta(' 1 day'),
    'workers' : {
        'Person1' : 'Owner',
        'Person2' : 'Boss',
        'Person3' : 'Boss',
    }
})

Inside Django admin forms, for every field is generated an input, but for the field "information_as_json", I don't want to show it as a string or as JSON. That is because the users who are accessing this store admin page, need to read the field 'information_as_json' easier since no one can edit these values because it is generated in another part of the application.

Is it possible to convert these values to a "div" or a plain text? The contents would be:

This store opens at: {information_as_json.open_at}

This store close at: {information_as_json.close_at}

And for the workers, iterate through keys and values:

for key, value in information_as_json.workers:

Worker {key} has the role: {value}

I'm a beginner at Django, so I'm struggling a little with this part.

Every help would be appreciated :D

CodePudding user response:

I would suggest approaching the model a little differently. Rather than storing the opening and closing hours as JSON they can just be fields directly on the store model. The the workers can be a Using a method in the Django admin as a read-only field

  • Related