如何列出djongo中与所有外键关联的所有值

huangapple go评论89阅读模式
英文:

How to list all values associated with all foreign keys in djongo

问题

我使用 djongo 作为连接 mongoDB 数据库的连接器。我创建了两个模型,分别为 MailGroupUser。每个 User 与一个 MailGroup 相关联。每个 MailGroup 可以拥有多个 User。现在,我想以以下格式获取所有的邮件组和用户信息:

  1. mailGroups = [
  2. {
  3. "id": 1,
  4. "name": "group - 1",
  5. "users": [
  6. {
  7. "name": "x1",
  8. "email": "y1"
  9. },
  10. {
  11. "name": "x2",
  12. "email": "y2"
  13. },
  14. {
  15. "name": "x3",
  16. "email": "y3"
  17. }
  18. ]
  19. },
  20. {
  21. "id": 2,
  22. "name": "group - 2",
  23. "users": [
  24. {
  25. "name": "a1",
  26. "email": "b1"
  27. },
  28. {
  29. "name": "a2",
  30. "email": "b2"
  31. }
  32. ]
  33. }
  34. ]

我尝试了以下代码,但只获取了邮件组,没有获取到与每个组相关联的用户信息:

  1. mailGroups = MailGroup.objects.all()
  2. serializer = MailGroupSerializer(mailGroups, many=True)

你应该如何处理这个问题?以下是我的模型定义:

  1. class MailGroup(models.Model):
  2. id = models.AutoField(primary_key=True)
  3. name = models.CharField(max_length=255)
  4. class User(models.Model):
  5. id = models.AutoField(primary_key=True)
  6. email = models.CharField(max_length=255)
  7. name = models.CharField(max_length=255)
  8. mailgroup = models.ForeignKey(MailGroup, on_delete=models.CASCADE)

这是我的序列化器定义:

  1. class MailGroupSerializer(serializers.ModelSerializer):
  2. class Meta:
  3. model = MailGroup
  4. fields = ["id", "name"]
  5. class UserSerializer(serializers.ModelSerializer):
  6. class Meta:
  7. model = User
  8. fields = ["name", "email"]

要获取与每个邮件组相关联的用户信息,你需要使用嵌套序列化器来实现。以下是如何修改你的代码:

  1. mailGroups = MailGroup.objects.all()
  2. mailGroupData = []
  3. for mailGroup in mailGroups:
  4. users = User.objects.filter(mailgroup=mailGroup)
  5. userSerializer = UserSerializer(users, many=True)
  6. mailGroupData.append({
  7. "id": mailGroup.id,
  8. "name": mailGroup.name,
  9. "users": userSerializer.data
  10. })
  11. # 现在 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

  1. mailGroups = [
  2. {
  3. "id": 1,
  4. "name": "group - 1",
  5. "users": [
  6. {
  7. "name": "x1",
  8. "email": "y1"
  9. },
  10. {
  11. "name": "x2",
  12. "email": "y2"
  13. },
  14. {
  15. "name": "x3",
  16. "email": "y3"
  17. }
  18. ]
  19. },
  20. {
  21. "id": 2,
  22. "name": "group - 2",
  23. "users": [
  24. {
  25. "name": "a1",
  26. "email": "b1"
  27. },
  28. {
  29. "name": "a2",
  30. "email": "b2"
  31. }
  32. ]
  33. }
  34. ]

I tried this and got only the mail groups, not the associated users with each group

  1. mailGroups = MailGroup.objects.all()
  2. serializer = MailGroupSerializer(mailGroups, many = True)

How do I do this? Here are my models

  1. class MailGroup(models.Model):
  2. id = models.AutoField(primary_key=True)
  3. name = models.CharField(max_length=255)
  4. class User(models.Model):
  5. id = models.AutoField(primary_key=True)
  6. email = models.CharField(max_length=255)
  7. name = models.CharField(max_length=255)
  8. mailgroup = models.ForeignKey(MailGroup, on_delete=models.CASCADE)

These are my serializers:

  1. class MailGroupSerializer(serializers.ModelSerializer):
  2. class Meta:
  3. model = MailGroup
  4. fields = ["id", "name"]
  5. class RecipientSerializer(serializers.ModelSerializer):
  6. class Meta:
  7. model = Recipient
  8. fields = ["id", "email", "name", "mailGroup"]

答案1

得分: 1

你将users添加到了MailGroupSerializer中:

  1. class UserSerializer(serializers.ModelSerializer):
  2. class Meta:
  3. model = User
  4. fields = ['id', 'name']
  5. class MailGroupSerializer(serializers.ModelSerializer):
  6. users = UserSerializer(source='user_set', many=True)
  7. class Meta:
  8. model = MailGroup
  9. 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>

  1. class Meta:
  2. model = MailGroup
  3. fields = [&#39;id&#39;, &#39;name&#39;, &#39;users&#39;]&lt;/code&gt;&lt;/pre&gt;

huangapple
  • 本文由 发表于 2023年7月6日 21:36:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76629441.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定