[Python] ManyToOneField in Django

Discussão em 'Python' iniciado por Stack, Março 7, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to define a many-to-one field in the class that is the "Many". For example, imagine a situation where a user can only be a member of one group but a group can have many users:

    class User(models.Model):
    name = models.CharField()

    class Group(models.Model):
    name = models.CharField()
    # This is what I want to do -> users = models.ManyToOneField(User)


    Django docs will tell to define a group field in the User model as a ForeignKey, but I need to define the relationship in the Group model. As far as I know, there isn't a ManyToOneField and I would rather not have to use a ManyToManyField.

    Continue reading...

Compartilhe esta Página