Django - 阻止用户访问相同的值

时间:2017-03-25 19:39:46

标签: python django python-3.x django-models django-views

我有一个日历,假设它连接到当前的admins-association。管理员在日历中选择日期并注册表单以便设置事件。但是当我使用其他管理员关联登录时,我仍然可以看到上一个管理员的事件。我想让它们彼此分开。

仍然是一个新手,所以以正确的方式引导我,感谢你的帮助,伙计们!

models.py

class Administrator(AbstractUser):
        ...
        association = models.ForeignKey(Association)


class Event(models.Model):
        ...
        association = models.ForeignKey(Association)


class Association(models.Model):
        asoc_name = models.CharField(max_length=50, null=True, blank=True)

views.py

def event_add_edit(request):
   if request.method == 'POST':
       res = {'success': False}

       action = request.POST['action']
       name = request.POST['name']
       location = request.POST['location']
       start = request.POST['start']
       end = request.POST['end']
       allday = request.POST['allday'] == 'true'
       description = request.POST['description']
       synced = request.POST['synced'] == 'true'
       association = Association.objects.filter(asoc_name=request.user.association)
       asoc = Association.objects.get(id=association)

       if action == 'add':
           Event.objects.create(
              name=name,
              location=location,
              start=start,
              end=end,
              allday=allday,
              description=description,
              synced=synced,
              association=asoc
           )

           res['success'] = True
           res['message'] = 'added'
           eid = Event.objects.latest('id').id
           res['eid'] = eid
           res['data'] = Event.objects.values().get(id=eid)
       elif action == 'edit':


           eid = request.POST['eid']
           event = Event.objects.get(id=eid)
           event.name = name
           event.location = location
           event.start = start
           event.end = end
           event.allday = allday
           event.description = description
           event.synced = synced
           event.save()

           res['success'] = True
           res['message'] = 'edited'
           res['eid'] = eid
           res['data'] = Event.objects.values().get(id=eid)

       return JsonResponse(res)
   else:
       raise Http404

1 个答案:

答案 0 :(得分:0)

只收集仅来自您当前登录的关联的事件。