因此,我有一个名为“任务”的模型,并且该任务具有“ UserProfile”。这些“ UserProfile”模型只是普通用户,但在“任务”的应用程序内部。
这是我现在拥有的API:
"tasks": [
{
"id": 1,
"name": "Läs På Internet",
"description": "asdasdasdasa",
"created": "2019-06-08",
"deadline": "2019-06-19",
"state": "new",
"stickers": [
{
"id": 1,
"name": "Detta är en sticker",
"content": "Sillicon Valley",
"created": "2019-06-08",
"creator": {
"id": 1,
"user": 1
}
}
],
"checkmarks": [
{
"id": 1,
"name": "Googla",
"checked": false
}
],
"workers": [
{
"id": 1,
"user": 1
}
]
},
{
"id": 2,
"name": "Läs i böcker",
"description": "aaa",
"created": "2019-06-10",
"deadline": "2019-06-25",
"state": "done",
"stickers": [],
"checkmarks": [],
"workers": [
{
"id": 1,
"user": 1
}
]
}
],
如您所见,每个用户现在都具有“ id”和“ user”,它们都是ID。如何获取每个用户的username
并将其显示在其余的api中?
# Users
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
class Meta:
verbose_name_plural = 'All Users'
def __str__(self):
return self.user.username
@receiver(post_save, sender=User)
def create_user_data(sender, update_fields, created, instance, **kwargs):
if created:
user = instance
profile = UserProfile.objects.create(user=user)
#Tasks
class Task(models.Model):
name = models.CharField(max_length=200)
description = models.CharField(max_length=2000)
created = models.DateField(default=date.today)
deadline = models.DateField(default=date.today)
state = models.CharField(max_length=20, default='new')
stickers = models.ManyToManyField(Sticker, blank=True)
checkmarks = models.ManyToManyField(Checkmark, blank=True)
workers = models.ManyToManyField(UserProfile, blank=True, related_name='TaskWorkers')
class Meta:
verbose_name_plural = 'Tasks'
def __str__(self):
return "{name}".format(name=self.name)
序列化器:
class UserProfileSerializer(serializers.ModelSerializer):
class Meta:
model = UserProfile
fields = ('id', 'user')
class TaskSerializer(serializers.ModelSerializer):
stickers = StickerSerializer(many=True, read_only=True)
checkmarks = CheckmarkSerializer(many=True, read_only=True)
workers = UserProfileSerializer(many=True, read_only=True)
class Meta:
model = Task
fields = ('id', 'name', 'description', 'created', 'deadline', 'state', 'stickers', 'checkmarks', 'workers')
观看次数:
class TaskView(viewsets.ModelViewSet):
http_method_names = ['get', 'post', 'put', 'delete', 'patch']
queryset = Task.objects.all()
serializer_class = TaskSerializer
class UserTaskView(TaskView):
def get_queryset(self):
return Task.objects.filter(workers__user=self.request.user)
用户视图仅用于显示已分配的用户!
答案 0 :(得分:1)
您可以使用以下方式更新user
内的UserProfileSerializer
序列化程序(使用您想要的User
字段创建序列化程序):
class UserProfileSerializer(serializers.ModelSerializer):
user = UserSerializer()
class Meta:
model = UserProfile
fields = ('id', 'user')
或此操作(从UserProfileSerializer
内部访问用户字段):
class UserProfileSerializer(serializers.ModelSerializer):
username = serializers.CharField(source='user.username')
class Meta:
model = UserProfile
fields = ('id', 'user', 'username')
答案 1 :(得分:-1)
如下文所述 https://www.django-rest-framework.org/api-guide/serializers/#writable-nested-representations 您可以通过以下方式获取任务内的用户个人资料详细信息:
class TaskSerializer(serializers.ModelSerializer):
......
user = UserSerializer()
class Meta:
model = Task
fields = (...., 'user',....., 'workers')
这将返回用户模型中的所有字段,作为任务嵌套json的一部分