0
내 Django 프로젝트에 속행 및 사용자 인 두 개의 모델이 있으며, 모든 후속 작업에는 사용자 인스턴스가 매핑됩니다 [액터 필드]. 무슨 일 나는 followups직렬화를 위해 쿼리 세트에서 중첩 된 객체의 특정 값 가져 오기
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)
지원 후속 모델
에서 가져온 모든 행에 대한 사용자 모델에있는 FIRST_NAME 필드를 얻을 필요가 나는이 코드를 실행할 때 기본적으로 배우의 기본 키를 얻을 수있다 장고가 지원 세리 does'nt 때문에class Followup(TimeStampedModel):
lead_name = models.ForeignKey(
LeadInfo,
on_delete=models.CASCADE,
null=True
)
followup = models.DateField(
blank=False,
verbose_name='Follow up date'
)
comments = models.TextField(blank=True, verbose_name='Comments')
actor = models.ForeignKey(
User,
blank=True,
limit_choices_to={'is_staff': True},
on_delete=models.CASCADE,
null=True,
verbose_name='Actor'
)
class Meta:
verbose_name = 'followup'
verbose_name_plural = 'followups'
def __str__(self):
return '{}'.format(self.lead_name)
사용자 모델
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()
https://stackoverflow.com/a/17032984/2282638 : –
당신에게 @SandeepBalagopal 감사드립니다. 그것은 장고에 내장 된 serializers에서 관련 필드에 대한 구문 분석을 지원하지 않습니다. 답변을 업데이트했습니다. – rajkris
https://docs.djangoproject.com/en/1.10/topics/serialization/#natural-keys –