0

I have class Team, which means a group of users. And the relation with User is One-to-Many.

class Team(models.Model):
        member = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)

But Team may consist of 2 and N members. I think to write manually is not our way, because it depends on count of people which is always variable.

 class Team(models.Model):
    member1 = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)   
    member2 = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)
    member3 = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)

How can I do it more elegant and rational? I mean to connect count variable and ForeignKeys associating with users.

upd 15:17 UTC. I still don't understand how to make this. Sorry. So, let's begin to draw. enter image description here

For example, I wrote simple code

class Event(models.Model):
    id_user = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)

But when I go to admin panel to add Event, I can add only one user. But I need to add a lot of users, who want to participate in the Event and then to divide them into Teams. I think I don't need another class Member. It seems unnecessary. Am I wrong?

Sergo
  • 93
  • 3
  • 10
  • 1
    If each user can only have one team, make the users attach to teams (ie put a team foreign key in the user object). If it is many-to-many, create a TeamConnection object which connects users and teams. – ifly6 Jun 22 '22 at 13:22

2 Answers2

2

Create a team with one field team_name:

class Team(models.Model):
    team_name = models.CharField(max_length=1000) 

and assign users to this team:

class User(models.Model):
    this_users_team = models.ForeignKey(Team, null=True, on_delete=models.SET_NULL) 

in this case you can assign as many Users to any team, as you want

oruchkin
  • 1,145
  • 1
  • 10
  • 21
  • Thx, but I haven't Uer class. I'm using User from django.contrib.auth.models import User.. – Sergo Jun 22 '22 at 13:54
2

As you stated Team model, have ManyToOne relation that means ForeignKey.

But Team may consist of 2 and N members.

You should create two models Team and Member. In Member model, you can store all information related to members such as their age, gender, city, etc.

In Team model, you can store information related to particular team which consists of more than one Member i.e. It has ManyToOne relation with Member.

Models.py:

from django.db import models
from django.contrib.auth.models import User


class Member(models.Model):
    user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)

    def __str__(self) -> str:
        return f"{self.user.username}"


class Team(models.Model):
    member = models.ForeignKey(Member, on_delete=models.SET_NULL, null=True)

Registering in admin site:

admin.py:


@admin.register(Member)
class MemberAdmin(admin.ModelAdmin):
    list_display = ['id', 'user'] #Also specify other fields.


@admin.register(Team)
class TeamAdmin(admin.ModelAdmin):
    list_display = ['id', 'member'] #Also specify other fields.



Edit:

According to the current picture, a Event can have more than one Team as well as more than one User. So, make two separate Foreign keys for both the models.

models.py

from django.db import models
from django.contrib.auth.models import User


class Team(models.Model):
    # The below field is for name of team
    name = models.CharField(max_length=200)
    # The below field is for member of team.
    user = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)

    def __str__(self) -> str:
        return f"{self.name}"


class Event(models.Model):
    # The below field is for name of event.
    name = models.CharField(max_length=200)
    # The below field is for user of event.
    user = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)
    # The below is for team of event.
    team = models.ForeignKey(Team, on_delete=models.SET_NULL, null=True)

    def __str__(self) -> str:
        return f"{self.name}"

admin.py

from django.contrib import admin
from .models import Team, Event


@admin.register(Event)
class EventAdmin(admin.ModelAdmin):
    list_display = ['id', 'name', 'user', 'team']


@admin.register(Team)
class TeamAdmin(admin.ModelAdmin):
    list_display = ['id', 'name', 'user']

views.py

from django.shortcuts import render
from .models import Team, Event


def home(request):
    events = Event.objects.all()

    return render(request, 'any_app_name/home.html', {'events': events})

home.html or template file:

<body>
    <h2>All team information.</h2>

    <div>
        {% for event in events %}
        <h3>{{forloop.counter}}</h3>
        <p>Name of event: {{event.name}}</p>
        <p>Name of user related to this event: {{event.user.username}}</p>
        <p>Name of team related to this event: {{event.team.name}}</p>
        <br><hr>
        {% endfor %}
    </div>
</body>
Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
  • Thank you, but how can I restrict user to apply differect class simultaneously during an Event, for example. I mean. I have Event, where several teams can participate. And one user can choose only one team. But. in another Event the user can choose another Team for the new Event. – Sergo Jun 22 '22 at 14:02
  • 1
    @Sergo I think you should make a Event model for that. – Sunderam Dubey Jun 22 '22 at 14:03
  • Sunderam Dubey, thank you, but I'm sorry I didn't catch. Can you please explain if I have used User from 'django.contrib.auth.models import User' can I create a User class without overwriting the django class? I afraid errors, because I have importand data in my database. – Sergo Jun 22 '22 at 14:37
  • 1
    @Sergo And yes, you can do, make a similar foreign key of member in Event Model. – Sunderam Dubey Jun 22 '22 at 14:38
  • I draw it in Paint:) I have one question. I see some opportuinities with your class Member. But how can I add new users automatically to class Member. I can do it only manually right now. It's not convenient. (I upvoted all your answers) – Sergo Jun 23 '22 at 20:01
  • 1
    @Sergo To automatically add member you can use [`post_save`](https://docs.djangoproject.com/en/4.0/ref/signals/#post-save) signal. – Sunderam Dubey Jun 24 '22 at 08:46