How to register more models via django admin in the same template

Hello everyone,

I’m developing an app and my models are linked in such a way that I can’t register them differently. So I would like to have a way to register them in the same template. My models are below:

class Candidat(models.Model):
    user = models.OneToOneField(User, on_delete= models.CASCADE)
    id_moyenne_matiere = models.ForeignKey(MoyenneMatiere, on_delete= models.CASCADE)
    id_moyenne_module = models.ForeignKey(MoyenneModule, on_delete= models.CASCADE)
    id_moyenne_candidat = models.ForeignKey(MoyenneCandidat, on_delete= models.CASCADE)
    id_niveau = models.ForeignKey(Niveau, on_delete= models.CASCADE)
    id_filiere = models.ForeignKey(Filiere, on_delete= models.CASCADE)

class MoyenneCandidat(models.Model):
    libelle = models.FloatField()

class MoyenneModule(models.Model):
    libelle = models.FloatField()
    id_moyenne_candidat = models.ForeignKey(MoyenneCandidat, on_delete= models.CASCADE)
    id_module = models.ForeignKey(Module, on_delete= models.CASCADE)
class Note(models.Model):
    libelle = models.FloatField()
    id_moyenne_candidat = models.ForeignKey(MoyenneCandidat, on_delete= models.CASCADE)
    id_examen_annee = models.ForeignKey(ExamenAnnee, on_delete= models.CASCADE)

Hope you can help me

I don’t understand what you mean by “registering” them here.

You can create multiple forms in a view, one form for each model. You can then pass both those forms in your context being used to render a template. Your template can then render all the forms.

Also see Prefixes for forms.

Sorry, I should write objects instead of models. I mean how can I register objects in my models. May I customize Django admin to allow me to add objects in my models in this way : In one template, I’ll be able to add more objects of different models which are linked among them.

Sorry, but I’m still not clear what you mean when you’re using the word “register” here.

If you’re talking about creating new models through the admin, no. Django doesn’t work like that.

If you’re talking about adding the data for the models, that’s what the Django admin is designed to do. There is some functionality provided in the admin for adding related data to a model on the same page.

Yes I am talking about adding data to more than one model in the same template using Django admin. Is there any functionality that can do this?

Yes, if you register your model with the admin, any foreign key fields are shown as select lists. You’ll see a green “+” (plus sign) next to the select list. Clicking on the “+” brings up a dialog box where you can add entries in the related model.

Or, if you’re working with a “reverse ForeignKey” situation, you also have the InlineModelAdmin facility for managing related models.

1 Like

Thank you for your help. It’s exactly what I need.