TL;DR
If I understood you do not need the inline, just register the Student and Teacher normally in admin, see if this is it:
py.models:
from django.db import models
from django.contrib.auth.models import User
class Aluno(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
# Adicionando os campos
matricula = models.CharField('Matrícula', max_length=12)
def __str__(self):
return self.user.username
class Professor(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
# Adicionando os campos
disciplina = models.CharField('Disciplina', max_length=70)
def __str__(self):
return self.user.username
admin py.
from django.contrib import admin
from .models import Aluno, Professor
@admin.register(Aluno)
class AlunoAdmin(admin.ModelAdmin):
model = Aluno
list_display = ('user', 'matricula',)
@admin.register(Professor)
class ProfessorAdmin(admin.ModelAdmin):
model = Professor
list_display = ('user', 'disciplina',)
Your admin screen should look like this:
The inclusion of a Student, for example:
These screens captured repl.it, if you want to test there, see the link I leave below, remembering that to run Oce first need to make a project Fork, then, in the frame on the right, need to create the migrations, make the migrate, create a superuser and finally run.
Adding inlines
in the User
:
With inlines
you can achieve on a single screen, in which you can make the inclusion of User
, the Professor
and the Aluno
, but in my opinion, it gets a little strange, see that inlines is to show record(s) of the relationship between one class and another, see that I put in plural, for example when Voce wants to show the student data and his grades over a period. The inline
, by default, always comes plural, in this case your inclusion screen User
would look like in the figure below (Then add the changes in the code you would have to do for this):
I said it gets weird, why? Because inline
in this case will request the two relationships, no matter if you are registering a teacher or a student, will appear the fields for typing the two information, but probably a User
will relate to a Professor
OR to a Aluno
, never both at once, it gets weird.
Note that in this case I just re-registered the User
in admin, the other options continue as before, for that just needed to change the file admin.py
:
from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from .models import Aluno, Professor
class AlunoInline(admin.StackedInline):
model = Aluno
can_delete = False
verbose_name = 'Aluno'
class ProfessorInline(admin.StackedInline):
model = Professor
can_delete = False
verbose_name = 'Professor'
class UserAdmin(BaseUserAdmin):
inlines = (AlunoInline, ProfessorInline)
# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
@admin.register(Aluno)
class AlunoAdmin(admin.ModelAdmin):
model = Aluno
list_display = ('user', 'matricula',)
@admin.register(Professor)
class ProfessorAdmin(admin.ModelAdmin):
model = Professor
list_display = ('user', 'disciplina',)
If Voce wants to avoid plural in inline (From Alunos
p/ Aluno
and/or Professores
p/ Professor
) adding in models
the class Meta
, and change the verbose_name_plural
, for example:
class Professor(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
# Adicionando os campos
disciplina = models.CharField('Disciplina', max_length=70)
class Meta:
verbose_name_plural = 'Professor'
def __str__(self):
return self.user.username
See working on repl.it.
If I understood your problem correctly, wouldn’t it be ideal to go back and model your database so that the differences between one and the other is only one line to identify which type of user? For example, the Student, Manager and Teacher classes inherit from the User class (which is quite complete).
– Paulo Vinícius
Anyway, here is an answer in the OS regarding your problem and the error: https://stackoverflow.com/questions/33748059/add-inline-model-to-django-admin-site
– Paulo Vinícius
@Thanks for the tip. I saw this answer there but apparently it’s the same as mine. The problem is to define this union with the User class. Even if I remodel I still need an area to register the Manager, the collaborator and the Student. I just want to register each one separately and without having to change the screen to register the User.
– Flávio Filipe
I don’t know if it makes a difference, but they’re using keys instead of parentheses to assign inlines. Another thing, that last line of the answer "admin.site.Register(Rule,Ruleadmin)" goes in the admin.py file, although the person who responded did not mention it. But it’s only in this that I can help, I’m not a deep connoisseur of Jango.
– Paulo Vinícius