我有一个约为door
和door_group
s的权限模型。
door
和door_group
模型之间存在ManyToMany关系。他们与permission
的OneToMany相关。
我想问一下,我怎样才能获得所有door
特定的不同权限?
permissions_list = []
permissions_list.append(door.permission_set.all())
for group in door.door_group_set.all():
permissions_list.append(group.permission_set.all())
serializer = PermissionSerializer(permissions_list, many=True)
return Response(serializer.data)
我获得了所有权限,但我无法使用ModelSerializer
类序列化它。
权限模型:
class Permission(models.Model):
user = models.ForeignKey('users.EmailUser')
door = models.ForeignKey('doors.Door', null=True, blank=True)
door_group = models.ForeignKey('doors.Door_group', null=True, blank=True)
days = models.CharField(validators=[validate_comma_separated_integer_list], max_length=13, null=True)
start_hour = models.TimeField(default='00:00')
end_hour = models.TimeField(default='00:00')
created_date = models.DateTimeField(default=timezone.now)
updated_date = models.DateTimeField(auto_now=True)
def __unicode__(self):
return 'Days: ' + self.days + ' Start:' + str(self.start_hour) + ' End: ' + str(self.end_hour) + ' User: '+ self.user.username
def clean(self):
# Don't allow draft entries to have a pub_date.
if self.door_group is not None and self.door is not None:
raise ValidationError('Choose between, group of doors or one door to give permission. Can not choosable both.')
if self.door_group is None and self.door is None:
raise ValidationError('Choose between, group of doors or one door to give permission.')