在Django中将对象分配给特定用户?

时间:2019-02-21 12:56:22

标签: python django object project assign

尝试使我的matches.html页面显示特定于用户的时间表数据,但是当我登录另一个用户时,以前的用户数据仍然存在。 有没有一种方法可以使时间表数据分配给用户登录。

class timetablesetup(models.Model):
    module = models.CharField(max_length=150)
    day = models.CharField(max_length=100)
    time = models.IntegerField()
    location = models.CharField(max_length=100)


    def __str__(self):
        return self.module

那是我的models.py,这是视图

def matches(request, user):
    entrys = timetablesetup.objects.all()
    context = {
        'entrys': entrys
    }
    return render(request, 'timetableMatch/matches.html', context)

def add_entry(request):
    return render(request, 'timetableMatch/add_entry.html')

def delete(request, id):
    entrys = timetablesetup.objects.get(pk=id)
    entrys.delete()
    return redirect('/timetableMatch')

def edit(request, id):
    entrys = timetablesetup.objects.get(pk=id)
    context = {
        'entrys': entrys
    }
    return render(request, 'timetableMatch/edit.html', context)

def update(request, id):
    entrys = timetablesetup.objects.get(pk=id)
    entrys.module = request.GET['module']
    entrys.day = request.GET['day']
    entrys.time = request.GET['time']
    entrys.location = request.GET['location']
    try:
        entrys.save()
    except Exception:
        return redirect('/errorpage')
    else:
        return redirect('/timetableMatch')

这是我的比赛页面,用于查看时间表数据

<div class="container"><br>
    <h2>TimetableMatch</h2>
    <hr>
    <table class="table table-dark">
        <thead>
            <tr>
                <th>Modules</th>
                <th>Day</th>
                <th>Time</th>
                <th>Location</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
          {% for entry in entrys %}
                <tr>
                <td>{{ entry.module }}</td>
                <td>{{ entry.day }}</td>
                <td>{{ entry.time }}</td>
                <td>{{ entry.location }}</td>
                <td><a class="btn btn-info" href="../edit/{{ entry.id }}">Edit</a>
                    <a class="btn btn-danger" href="../delete/{{ entry.id }}">Delete</a>
                </td>
            </tr>
            {% endfor %}
        </tbody>
    </table>

1 个答案:

答案 0 :(得分:0)

您应该使用ForeignKey将每个TimeTableSetup对象链接到数据库中的用户,然后在匹配视图中进行过滤以确保显示给定用户的时间表:

class timetablesetup(models.Model):
    module = models.CharField(max_length=150)
    day = models.CharField(max_length=100)
    time = models.IntegerField()
    location = models.CharField(max_length=100)
    user = models.ForeignKey(<POINT TO YOUR USER MODEL HERE>)


    def __str__(self):
        return self.module

def matches(request, user):

    # Get the user whose entries you want to show here
    # E.g., user = User.objects.get(id=3)
    user = <GET THE USER HERE>

    # Filter entrys to only those assigned to a certain user
    entrys = timetablesetup.objects.filter(user=user)

    context = {
        'entrys': entrys
    }
    return render(request, 'timetableMatch/matches.html', context)