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:
- Go to django admin page
- Create a teacher
- Create a student
- 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?