使用一对多关系在Django中创建2种不同的用户类型

时间:2018-08-16 17:11:30

标签: python django

我正在创建一个篮球统计应用程序,在这里我希望拥有两种不同的用户类型,即Coach和Player。我希望教练只能登录并查看其球员的统计信息。因此,一位教练将有很多球员。我希望能够为每个教练创建用户个人资料,并仅显示其球员,而不是数据库中的所有球员。到目前为止,我已经创建了显示所有注册用户的用户配置文件,但是我对如何分隔两个用户感到困惑,以至于球员将不在指导之下。另外,如何显示HTML教练中列出的所有球员?谢谢!!!

Models.py视图:

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


class UserProfile(models.Model):
name = models.CharField(max_length=100, default='')
school_name = models.CharField(max_length=100, default='')
user = models.OneToOneField(User, on_delete=models.CASCADE)
description = models.CharField(max_length=100, default='')
city = models.CharField(max_length=100, default='')
state = models.CharField(max_length=100, default='')
website = models.URLField(default='')
phone = models.IntegerField(default=0)
jersey_number = models.IntegerField(default=0)
image = models.ImageField(upload_to='profile_image', blank=True)
position = models.CharField(max_length=100, default='')
height = models.CharField(max_length=100, default='')
weight = models.CharField(max_length=100, default='')
grade = models.CharField(max_length=100, default='')
background_image = models.ImageField(upload_to='profile_image', blank=True)

def __str__(self):
    return self.user.username


def create_profile(sender, **kwargs):
if kwargs['created']:
    user_profile = UserProfile.objects.create(user=kwargs['instance'])

post_save.connect(create_profile, sender=User)

Views.py:

@login_required(login_url='login')
def view_profile(request, pk=None):
if pk:
    user = User.objects.get(pk=pk)
else:
    user = request.user
args = {'user': user}
return render(request, 'accounts/profile.html', args)

因此,我以用户身份登录,并且显示了两个玩家。我希望它只显示player1,然后以其他教练的身份登录,并显示player2。

[![在此处输入图片描述] [1]] [1]

Django Admin视图: 因此,如果我在每个用户下登录,它将显示所有用户。例如:我想以1位用户(教练)身份登录并显示1-4号球员,然后以另一位教练身份登录,并显示5-7号球员。 [![在此处输入图片描述] [2]] [2]

HTML视图:

<div class="column middle">
                <table id="roster" class="table table-bordered table-hover table-condensed"><!--add roster-->
                    <h2>Roster</h2>
                    <thead>
                        <tr>
                            <th scope="col">#</th>
                            <th scope="col">Player Name</th>
                            <th scope="col">Position</th>
                            <th scope="col">Grade</th>
                        </tr>
                    </thead>
                    <tbody>
                                {% for user in users %}
                        <tr>
                            <td class="number">{{ user.userprofile.jersey_number }} </td>
                            <td class="name">
                                {% if user.userprofile.image %}
                                <img src="{{ user.userprofile.image.url }}" width="60" height="75" vspace="10">
                                {% endif %}
                                <a href="{% url 'view_profile_with_pk' pk=user.pk %}">
                                {{ user.userprofile.name }}
                                </a>
                            </td>
                            <td class="position">{{ user.userprofile.position }}</td>
                            <td class="grade">{{ user.userprofile.grade }}</td>
                        </tr>
                                {% endfor %}

                    </tbody>
                </table>
            </div>

1 个答案:

答案 0 :(得分:1)

不要将教练和球员使用相同的模型,而是将他们分开。您还可以创建一个团队抽象概念,这是一个好主意,因为您可能希望能够每个团队拥有多位教练,或者让一位教练执教一年,而另一位教练执教下一年。

models.py

class Coach(models.Model):
    user = models.OneToOneField("auth.User")
    # ... Other info

class Team(models.Model):
     coach = models.OneToOneField(Coach)
     # ... Other info

class Player(models.Model):
     user = models.OneToOneField("auth.User")
     team = models.ForeignKey(Team)
     # ... Other info

然后,如果您想要一名球员教练:

player = Player.objects.get(...)
players_coach = player.team.coach

或者,如果您想获得教练员:

coach = Coach.objects.get(...)
coaches_players = coach.team.player_set.all()

这还使您可以轻松地更改教练与球员之间的关系,例如将教练与球队之间的外键更改为ManyToManyField,以便允许每支球队中的许多教练以及多年来的教练来指导多支球队。

-------------这里是您如何实现它----------------

@login_required(login_url='login')
def view_profile(request, pk=None):
    if pk:
        user = User.objects.get(pk=pk)
    else:
        user = request.user

    context = {}

    if user.coach is not None:
        # User is a coach
        coach = user.coach
        coaches_players = coach.team.player_set.all()
        context["coaches_players"] = coaches_players
    elif user.player is not None:
        # User is a player
        player = user.player
        players_coach = player.team.coach
        context["players_coach"] = players_coach

    # Depending on which variables are None and which aren't, the user is a coach or a player.
    # If 'player' and 'players_coach' aren't None, then the user is a player and you now have their player object in 'player' and their coach in 'players_coach'.
    # If 'coach' and 'coaches_players' aren't None, then the user is a coach and you now have their coach object in 'coach' and their players in 'coaches_players'.

    context["user"] = user
    return render(request, 'accounts/profile.html', context)

然后使用HTML模板:

{% for player in coaches_players %}
<p>{{ player }}</p>
{% endfor %}