正在获取queryset中嵌套对象的特定值以进行序列化

2024-05-16 11:40:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我在django项目中有两个模型,分别是Followup和User,每个Followup都有一个映射到它的用户实例[actor field]。当我运行这段代码时,我得到了actors的主键,我需要获取first_name字段,该字段位于用户模型中,用于从后续操作中获取的所有行

result = Followup.objects.filter(lead_name = lead).only('lead_name','followup','comments','actor')
plan = PlanType.objects.filter(lead_id = lead)
response["followup"] = serializers.serialize('json', result)

后续模型

^{pr2}$

用户模型

class User(AbstractBaseUser, PermissionsMixin):
first_name = models.CharField(_('First Name'), max_length=120, blank=True)
last_name = models.CharField(_('Last Name'), max_length=120, blank=True)
email = models.EmailField(_('email address'), unique=True, db_index=True)
is_staff = models.BooleanField(_('staff status'), default=False,
                               help_text='Designates whether the user can log into this admin site.')

is_active = models.BooleanField('active', default=True,
                                help_text='Designates whether this user should be treated as '
                                          'active. Unselect this instead of deleting accounts.')
date_joined = models.DateTimeField(_('date joined'), default=timezone.now)

USERNAME_FIELD = 'email'
objects = UserManager()

class Meta:
    verbose_name = _('user')
    verbose_name_plural = _('users')
    ordering = ('-date_joined', )

def __str__(self):
    return str(self.email)

def get_full_name(self):
    """
    Returns the first_name plus the last_name, with a space in between.
    """
    full_name = '{} {}'.format(self.first_name, self.last_name)
    return full_name.strip()

def get_short_name(self):
    "Returns the short name for the user."
    return self.first_name.strip()

Tags: the用户name模型selftruedefaultobjects