英文:
How to list all values associated with all foreign keys in djongo
问题
我使用 djongo
作为连接 mongoDB
数据库的连接器。我创建了两个模型,分别为 MailGroup
和 User
。每个 User
与一个 MailGroup
相关联。每个 MailGroup
可以拥有多个 User
。现在,我想以以下格式获取所有的邮件组和用户信息:
mailGroups = [
{
"id": 1,
"name": "group - 1",
"users": [
{
"name": "x1",
"email": "y1"
},
{
"name": "x2",
"email": "y2"
},
{
"name": "x3",
"email": "y3"
}
]
},
{
"id": 2,
"name": "group - 2",
"users": [
{
"name": "a1",
"email": "b1"
},
{
"name": "a2",
"email": "b2"
}
]
}
]
我尝试了以下代码,但只获取了邮件组,没有获取到与每个组相关联的用户信息:
mailGroups = MailGroup.objects.all()
serializer = MailGroupSerializer(mailGroups, many=True)
你应该如何处理这个问题?以下是我的模型定义:
class MailGroup(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=255)
class User(models.Model):
id = models.AutoField(primary_key=True)
email = models.CharField(max_length=255)
name = models.CharField(max_length=255)
mailgroup = models.ForeignKey(MailGroup, on_delete=models.CASCADE)
这是我的序列化器定义:
class MailGroupSerializer(serializers.ModelSerializer):
class Meta:
model = MailGroup
fields = ["id", "name"]
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ["name", "email"]
要获取与每个邮件组相关联的用户信息,你需要使用嵌套序列化器来实现。以下是如何修改你的代码:
mailGroups = MailGroup.objects.all()
mailGroupData = []
for mailGroup in mailGroups:
users = User.objects.filter(mailgroup=mailGroup)
userSerializer = UserSerializer(users, many=True)
mailGroupData.append({
"id": mailGroup.id,
"name": mailGroup.name,
"users": userSerializer.data
})
# 现在 mailGroupData 包含了所需的数据
这样你就可以获取每个邮件组以及与之相关联的用户信息了。
英文:
I am using djongo
as database connector for mongoDB
. I created two models called MailGroup
and User
. Every User
is associated with a MailGroup
. Each MailGroup
can have multiple users
. Now, I want to fetch all mailgroups and users in the following format
mailGroups = [
{
"id": 1,
"name": "group - 1",
"users": [
{
"name": "x1",
"email": "y1"
},
{
"name": "x2",
"email": "y2"
},
{
"name": "x3",
"email": "y3"
}
]
},
{
"id": 2,
"name": "group - 2",
"users": [
{
"name": "a1",
"email": "b1"
},
{
"name": "a2",
"email": "b2"
}
]
}
]
I tried this and got only the mail groups, not the associated users with each group
mailGroups = MailGroup.objects.all()
serializer = MailGroupSerializer(mailGroups, many = True)
How do I do this? Here are my models
class MailGroup(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=255)
class User(models.Model):
id = models.AutoField(primary_key=True)
email = models.CharField(max_length=255)
name = models.CharField(max_length=255)
mailgroup = models.ForeignKey(MailGroup, on_delete=models.CASCADE)
These are my serializers:
class MailGroupSerializer(serializers.ModelSerializer):
class Meta:
model = MailGroup
fields = ["id", "name"]
class RecipientSerializer(serializers.ModelSerializer):
class Meta:
model = Recipient
fields = ["id", "email", "name", "mailGroup"]
答案1
得分: 1
你将users
添加到了MailGroupSerializer
中:
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ['id', 'name']
class MailGroupSerializer(serializers.ModelSerializer):
users = UserSerializer(source='user_set', many=True)
class Meta:
model = MailGroup
fields = ['id', 'name', 'users']
(请注意,我已经为你提供了代码的翻译部分。)
英文:
You add the users
to the MailGroupSerializer
:
<pre><code>class <b>UserSerializer</b>(serializers.ModelSerializer):
class Meta:
model = User
fields = ['id', 'name']
class MailGroupSerializer(serializers.ModelSerializer):
<b>users = UserSerializer(source='user_set', many=True)</b>
class Meta:
model = MailGroup
fields = ['id', 'name', 'users']</code></pre>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论