Lets say I have a model School
and another model Student
.
class Student(models.Model):
school = models.ForeignKey(School)
name = models.CharField(max_length=100)
When a school is clicked in admin, then a new page appears showing school model fields and values.
I also want to select the already available list of students in that page itself.
Inlines is different, they will allow the ability to create and edit new records(student) belonging to that school. But I don't want that, lets assume there are already many student records available. I should be able to select them in admin from that school model page.
Do you mean that for a given
School
instance you want to be able to get a list of all the students related to that school?In which case you use the
related_name
attribute of the ForeignKey relationship you specified. You haven't defined therelated_name
where you do:which is fine, it just uses the default related name which is the name of the child class (student) followed by _set
so for your school instance:
then you can pass that student queryset into your template.
You can disable the inline 'add' permission. See InlineModelAdmin