Django Admin Making Model Fields Required

2 min read

In this tutorial, we will learn how to make optional model fields required in Django admin to make sure that data for these fields are populated on the creation of new records from the Django admin interface.

 Understanding the Problem

Suppose you are working on a project that shares the User model with customers and Staff. In the models, there is a field phone_number that is not a required field because it's optional for customers to share their contact information. However, you want to make sure that each staff user should have a phone number.

There are multiple ways to achieve this. If you are totally sure that new staff members will only get added through the Django admin. Then the simplest one would be to make the phone_number field required in the admin form.

Making Fields Required In Django Admin

I am gonna use the blog project for this example. Here we want to make the summary field required without making changes in models.

Django Admin Making Model Fields Required

models.py

class Post(models.Model):
    title = models.CharField(max_length=200, unique=True)
    slug = models.SlugField(max_length=200, unique=True)
    author = models.ForeignKey(
        User, on_delete=models.CASCADE, related_name="blog_posts")
    updated_on = models.DateTimeField(auto_now=True)
    content = models.TextField()
    created_on = models.DateTimeField(auto_now_add=True)
    status = models.IntegerField(choices=STATUS, default=0)
    summary = models.CharField(max_length=500, null=True, blank=True)

    class Meta:
        ordering = ["-created_on"]

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        from django.urls import reverse
        return reverse("post_detail", kwargs={"slug": str(self.slug)})

This is how my admin.py file looks currently.

admin.py

@admin.register(Post)
class PostAdmin(SummernoteModelAdmin):
    list_display = ('title', 'slug', 'status', 'created_on')
    list_filter = ('status', 'created_on')
    search_fields = ['title', 'content']
    prepopulated_fields = {'slug': ('title',)}

In order to make the summary field required, we need to create a custom form for the Post model. I am making them on the same file you can do this on a separate forms.py file as well.

admin.py

class PostForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['summary'].required = True

    class Meta:
        model = Post
        fields = '__all__'

As intended we create the custom model form and in the __init__ method we made the summary field required.

Finally, we just need to set form = PostForm in PostAdmin.

This is how the admin.py file looks.

admin.py

class PostForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['summary'].required = True

    class Meta:
        model = Post
        fields = '__all__'


@admin.register(Post)
class PostAdmin(SummernoteModelAdmin):
    form = PostForm
    list_display = ('title', 'slug', 'status', 'created_on')
    list_filter = ('status', 'created_on')
    search_fields = ['title', 'content']
    prepopulated_fields = {'slug': ('title',)}

Save the files run the server and try to create a post without filling the summary field, you would receive a validation error like this.

 


DJANGO
author's image
Abhijeet Pal Author and Editor in Chief @djangocentral

Abhijeet is a full-stack software developer from India with a strong focus on backend and system design. He is driven by the need to create impactful solutions that add value to the internet in any way possible.

LinkedIn Twitter Github

Latest Articles

Latest from djangocentral

Django 4.1 adds async-compatible interface to QuerySet

The much-awaited pull request for an async-compatible interface to Queryset just got merged into the main branch of Django.Pull Request - https://github.com/django/django/pull/14843 The Django core team has been progressively adding async suppor…
Read more →

3 min read

Making Django Admin Jazzy With django-jazzmin

Django admin is undoubtedly one of the most useful apps of Django. Over the years there has been very little change in the admin app as far as the UX is concerned and it's not a bad thing at all. Django admin was designed to provide a simple and minimali…
Read more →

4 min read