2
나는 그것의 모든 관계를 가진 전체 객체를 직렬화하는 방법을 찾고 있습니다. 난 지금 serializer에서 기본 키를 얻을 수있을 것 같습니다. 나는 모든 인쇄를 실행 내가받는 사람 필드에 딱 기본 키를 나열하는 출력을 얻을 경우장고, 완전히 직렬화 된 개체를 얻으십시오. 많은 관계에서 많은 관계로의 모든 속성
all = serializers.serialize('xml', list(Action.objects.select_related().all()))
: 여기 시리얼 라이저를 실행
class Action(models.Model):
name = models.CharField('Action Name', max_length=250, unique=True)
message = models.TextField('Message', blank=True, null=True)
subject = models.CharField('Subject', max_length=40, null=True)
recipient = models.ManyToManyField('Recipient', blank=True, null=True)
def __str__(self):
return unicode(self.name).encode('utf-8')
def __unicode__(self):
return unicode(self.name)
class Recipient(models.Model):
address = models.EmailField('Address', max_length=75, unique=True)
businessHours = models.BooleanField('Business Hours Only', default=False)
def __str__(self):
return unicode(self.address).encode('utf-8')
def __unicode__(self):
return unicode(self.address)
:
이 내 모델입니다.
<field type="CharField" name="subject">On The Road Productions</field>
<field to="sla.recipient" name="recipient" rel="ManyToManyRel">
<object pk="29"></object>
</field></object>
<object pk="11" model="sla.action">
<field type="CharField" name="name">On The Road Productions</field>
<field type="TextField" name="message">
관련된 수신자의 모든 속성도 필요합니다. 어떻게 내가 이걸 얻을 수 있는지 아는가?
편집 - 나는 내가 xml_serializer.py에서이 조각을 발견
에서이 조각을 발견하지만 난 그것을 수정하는 방법을 모르겠어요.
def handle_m2m_field(self, obj, field):
"""
Called to handle a ManyToManyField. Related objects are only
serialized as references to the object's PK (i.e. the related *data*
is not dumped, just the relation).
"""
if field.creates_table:
self._start_relational_field(field)
for relobj in getattr(obj, field.name).iterator():
self.xml.addQuickElement("object", attrs={"pk" : smart_unicode(relobj._get_pk_val())})
self.xml.endElement("field")
관련 모델의 queryset을 얻을 수 있으며 너무 그들을 직렬화? –
오브젝트 사이의 관계를 유지 보수하는 것처럼 보이지 않습니다. – Ryan