4

Is there a Djangotastic way to display a default value for a field in the admin when there isn't a value? Like 'n/a', but not to save that to the database?

When I set all the fields in the model below to readonly in the admin, the front-end display looks like the image at the bottom. It feels visually collapsed like it should have a value or a box or something. If there isn't an easy way to do what I am looking for, then is there another solution to make the front-end admin more clear for the user?

class Package(models.Model):
    packaging_format = models.CharField(max_length=40)
    package_delivery_pattern = models.CharField(max_length=30, blank=True)
    package_delivery_comments = models.CharField(max_length=250, blank=True)
    package_manifest_filename = models.CharField(max_length=50)
    package_description = models.CharField(max_length=250, blank=True)
    package_naming_pattern = models.CharField(max_length=50)

Screenshot of fields as displayed in the admin: Screenshot of admin with readonly package fields

Dan Swain
  • 2,910
  • 1
  • 16
  • 36
jackiekazil
  • 5,696
  • 4
  • 21
  • 20

2 Answers2

6

What's happening is that your actually saving a empty string '' in your CharFields instead of None values (because of the blank=True). So the Django-admin is showing the string you saved in the db (in this case, nothing).

If you change your CharFields to null=True instead of blank=True, you will be saving NULL in your database instead of an empty string. And that way, you will get the behaviour you want.

EDIT: I know this solution is not recommended (following Django Docs), but that's the behaviour you wanted. Django-admin is just showing you the string you have in the database, which is ''.

Another solution that comes to my mind is to modify the ModelAdmin for your Package model, something like:

class PackageAdmin(admin.ModelAdmin):
    readonly_fields = ['show_package_delivery_pattern', ...]

    def show_package_delivery_pattern(self, obj):
        if obj.package_delivery_pattern:
            return obj.package_delivery_pattern
        else:
            return 'N/A'
    # same with all your CharFields..
juliomalegria
  • 24,229
  • 14
  • 73
  • 89
  • 2
    Docs say... " Avoid using null on string-based fields such as CharField and TextField unless you have an excellent reason. If a string-based field has null=True, that means it has two possible values for “no data”: NULL, and the empty string. In most cases, it’s redundant to have two possible values for “no data;” Django convention is to use the empty string, not NULL." | See: https://docs.djangoproject.com/en/dev/ref/models/fields/#null | It seems to me that front-end admin display is not a good enough reason to message with the normalization of the dataset. Your thoughts? – jackiekazil Nov 21 '11 at 17:42
  • Yes... modifying the model admin is kind of what I was thinking about. Thank you for updating your answer in response to my comment. – jackiekazil Nov 21 '11 at 18:36
3

As of Django 1.9 you can use empty_value_display at the site, model, or field level in the Django admin. At the model level:

class YourModelAdmin(admin.ModelAdmin):
    empty_value_display = '---'
Dan Swain
  • 2,910
  • 1
  • 16
  • 36