Django: How to add button to the object creation page?

Viewed 91

I have the following models:

from django.db import models

class Teacher(models.Model):
    name = models.fields.CharField(max_length=255)
    favorite_student = models.ForeignKey("Student", blank=True, null=True, on_delete=models.RESTRICT, related_name="+")

class Student(models.Model):
    name = models.fields.CharField(max_length=255)
    teacher = models.ForeignKey(Teacher, on_delete=models.CASCADE)


    def set_as_favorite_student(self):
        self.teacher.favorite_student = self

I've also registered both Teacher and Student in the admin page:

from .models import Teacher, Student

class TeacherAdmin(admin.ModelAdmin):
    list_display = (
        "id",
        "name",
        "favorite_student",
    )


class StudentAdmin(admin.ModelAdmin):
    list_display = (
        "id",
        "name",
        "teacher",
    )

Each teacher must have a favorite student. So when there is a new teacher, I do the following steps:

  1. Go to django admin page
  2. Create a teacher
  3. Create a student
  4. Go back to the teacher, and set favorite_student to the created student

Question: For step #3, how do I create a button next to existing buttons that is Save and set as favorite so that I can avoid step #4?

0 Answers
Related