Closed IsaiasSantos closed 10 years ago
How are you defining your resources and document?
################# My documents #####################
class Enderecos(EmbeddedDocument):
cep = StringField(max_length=8, required=True)
endereco = StringField(max_length=255, required=True)
bairro = StringField(max_length=100, required=True)
cidade = StringField(max_length=100, required=True)
estado = StringField(max_length=2, required=True)
class Contatos(EmbeddedDocument):
email = ListField(EmailField(max_length=100))
telefone_fixo = ListField(StringField(max_length=14))
telefone_movel = ListField(StringField(max_length=14))
radio = ListField(StringField(max_length=20))
skype = ListField(StringField(max_length=100))
fax = ListField(StringField(max_length=14))
class Pessoa(Document, EmbeddedDocument):
data_cadastro = DateTimeField(default=datetime.datetime.now, required=True)
enderecos = EmbeddedDocumentField(Enderecos)
contatos = EmbeddedDocumentField(Contatos)
meta = {'allow_inheritance': True}
class Fisica(Pessoa):
SEXO = (
('0', 'Masculino'),
('1', 'Feminio')
)
ESTADO_CIVIL = (
('0', 'Solteiro(a)'),
('1', 'Casado(a)'),
('2', 'Divorciado(a)'),
('3', 'Viuvo(a)')
)
nome = StringField(max_length=255, required=True)
rg = StringField(max_length=9)
cpf = StringField(max_length=11, required=True)
data_nascimento = DateTimeField()
sexo = StringField(max_length=1, choices=SEXO)
estado_civil = StringField(max_length=1, choices=ESTADO_CIVIL)
class Juridica(Pessoa):
proprietarios = ListField(ReferenceField(Fisica))
nome_fantasia = StringField(max_length=255, required=True)
razao_socail = StringField(max_length=255)
cnpj = StringField(max_length=14, required=True)
ie = StringField(max_length=12)
class PagamentoMeio(Document, EmbeddedDocument):
nome = StringField(max_length=100, required=True)
class PagamentoGrupo(Document, EmbeddedDocument):
TIPO = (
('0', 'Entrada'),
('1', 'Saida')
)
tipo = StringField(max_length=1, choices=TIPO, required=True)
nome = StringField(max_length=255, required=True)
class Pago(EmbeddedDocument):
data_baixa = DateTimeField(default=datetime.datetime.now)
data_pagamento = DateTimeField(default=datetime.datetime.now)
meio = EmbeddedDocumentField(PagamentoMeio)
multa = DecimalField(precision=2)
juros = DecimalField(precision=2)
desconto = DecimalField(precision=2)
valor = DecimalField(precision=2)
observacao = StringField(max_length=400)
class Cancelado(EmbeddedDocument):
data = DateTimeField(default=datetime.datetime.now)
motivo = StringField(max_length=255, required=True)
class Pagamento(Document):
pagador = EmbeddedDocumentField(Pessoa, required=True)
grupo = EmbeddedDocumentField(PagamentoGrupo, required=True)
meio = EmbeddedDocumentField(PagamentoMeio, required=True)
parcela = StringField(max_length=8, required=True)
valor = DecimalField(precision=2, required=True)
descricao = StringField(max_length=255, required=True)
pago = EmbeddedDocumentField(Pago)
cancelado = EmbeddedDocumentField(Cancelado)
data_cadastro = DateTimeField(default=datetime.datetime.now)
data_vencimento = DateTimeField(required=True)
############### My resources #######################
class PessoaFisicaResource(resources.MongoEngineResource):
class Meta:
object_class = Fisica
queryset = Fisica.objects.all()
allowed_methods = ('get', 'post', 'put', 'patch', 'delete')
authorization = authorization.Authorization()
paginator_class = paginator.Paginator
class PessoaJuridicaResource(resources.MongoEngineResource):
proprietarios = fields.ReferencedListField(of='pessoa.api.resources.PessoaFisicaResource', attribute='proprietarios', full=True, null=True)
class Meta:
object_class = Juridica
queryset = Juridica.objects.all()
allowed_methods = ('get', 'post', 'put', 'patch', 'delete')
authorization = authorization.Authorization()
paginator_class = paginator.Paginator
class PessoaResource(resources.MongoEngineResource):
class Meta:
object_class = Pessoa
queryset = Pessoa.objects.all()
allowed_methods = ('get', 'post', 'put', 'patch', 'delete')
always_return_data = True
resource_name = 'pessoas'
include_resource_type = True
authorization = authorization.Authorization()
prefer_polymorphic_resource_uri = True
polymorphic = {
'fisica': PessoaFisicaResource,
'juridica': PessoaJuridicaResource,
}
class PagoResource(resources.MongoEngineResource):
meio = fields.EmbeddedDocumentField(embedded='pagamento.api.resources.PagamentoMeioResource', attribute='meio', null=True)
class Meta:
object_class = Pago
class PagamentoGrupoResource(resources.MongoEngineResource):
class Meta:
object_class = PagamentoGrupo
queryset = PagamentoGrupo.objects.all()
allowed_methods = ('get', 'post', 'put', 'patch', 'delete')
always_return_data = True
resource_name = 'grupos'
authorization = authorization.Authorization()
class PagamentoMeioResource(resources.MongoEngineResource):
class Meta:
object_class = PagamentoMeio
queryset = PagamentoMeio.objects.all()
allowed_methods = ('get', 'post', 'put', 'patch', 'delete')
always_return_data = True
resource_name = 'meios'
authorization = authorization.Authorization()
class PagamentoResource(resources.MongoEngineResource):
meio = fields.EmbeddedDocumentField(embedded='pagamento.api.resources.PagamentoMeioResource', attribute='meio')
grupo = fields.EmbeddedDocumentField(embedded='pagamento.api.resources.PagamentoGrupoResource', attribute='grupo')
#pagador = fields.EmbeddedDocumentField(embedded='pessoa.api.resources.PessoaResource', attribute='pagador')
#pagador = fields.EmbeddedDocumentField(embedded='pessoa.api.resources.PessoaFisicaResource', attribute='pagador')
pagador = fields.EmbeddedDocumentField(embedded='pessoa.api.resources.PessoaJuridicaResource', attribute='pagador')
pago = fields.EmbeddedDocumentField(embedded='pagamento.api.resources.PagoResource', attribute='pago', null=True)
class Meta:
queryset = Pagamento.objects.all()
allowed_methods = ('get', 'post', 'put', 'patch', 'delete')
always_return_data = True
authorization = authorization.Authorization()
resource_name = 'pagamentos'
filtering = {
'id': ALL
}
Your code is strange. You are missing allowing inheritance. Then you have documents which extend both Document
and EmbeddedDocument
.
See example here how to define embeddable polymorphic resource.
You also did not tell what is the request you are doing.
Hello, I'm brazilian sorry if you have written something wrong.
I have a probmea a long time, how to make a MongoEngineResource Embedded field with a polymorphic
Eeste the error is reported in POST:
"{" error_message ":" <class'pessoa.models.Pessoa'> "," traceback ":" Traceback (most recent call last): \ n \ n File \ "/ home / isaiah / Projects / cedenti / venv/lib/python2.7/site-packages/tastypie/resources.py \ ", line 201, in wrapper \ n response = callback (request, * args, * kwargs) \ n \ n File \" / home / isaias/Projetos/cedenti/venv/lib/python2.7/site-packages/tastypie/resources.py \ ", line 432, in dispatch_list \ n return self.dispatch ('list', request, * kwargs) \ n \ n File \"/home/isaias/Projetos/cedenti/venv/lib/python2.7/site-packages/tastypie_mongoengine/resources.py\", line 390, in dispatch \ n return self._wrap_request (request, lambda:. super (MongoEngineResource, self) dispatch (request_type, request, * kwargs)) \ n \ n File \"/home/isaias/Projetos/cedenti/venv/lib/python2.7/site-packages/tastypie_mongoengine/resources.py\", line 360, in _wrap_request \ n return func () \ n \ n File \"/home/isaias/Projetos/cedenti/venv/lib/python2.7/site-packages/tastypie_mongoengine/resources.py\", line 390, in \ n return self._wrap_request (request, lambda: super (MongoEngineResource, self) dispatch (request_type, request, * kwargs).) \ n \ n File \ "/ home / isaiah / Projects / cedenti/venv/lib/python2.7/site-packages/tastypie/resources.py \ ", line 464, in dispatch \ n response = method (request, \ kwargs) \ n \ n File \" / home / isaias / Projetos/cedenti/venv/lib/python2.7/site-packages/tastypie/resources.py \ ", line 1348, in post_list \ n = updated_bundle self.full_dehydrate (updated_bundle) \ n \ n File \"/home/isaias/Projetos/cedenti/venv/lib/python2.7/site-packages/tastypie_mongoengine/resources.py\", line 428, in full_dehydrate \ n return super (MongoEngineResource, self). full_dehydrate (bundle, for_list) \ n \ n File \ "/ home/isaias/Projetos/cedenti/venv/lib/python2.7/site-packages/tastypie / resources.py \ ", line 854, in full_dehydrate \ n bundle.data [field_name] = field_object.dehydrate (bundle, for_list for_list =) \ n \ n File \" / home / isaiah / Projects / cedenti / venv / lib / python2.7/site-packages/tastypie/fields.py \ ", line 732, in dehydrate \ n return self.dehydrate_related (fk_bundle, self.fk_resource, for_list for_list =) \ n \ n File \" / home / isaias / Projetos/cedenti/venv/lib/python2.7/site-packages/tastypie/fields.py \ ", line 570, in dehydrate_related \ n return related_resource.full_dehydrate (bundle) \ n \ n File \"/home/isaias/Projetos/cedenti/venv/lib/python2.7/site-packages/tastypie_mongoengine/resources.py\", line 432, in full_dehydrate \ n return super (MongoEngineResource, self). full_dehydrate (bundle, for_list) \ n \ n File \ "/ home/isaias/Projetos/cedenti/venv/lib/python2.7/site-packages/tastypie / resources.py \ ", line 860, in full_dehydrate \ n bundle.data [field_name] = method (bundle) \ n \ n File \"/home/isaias/Projetos/cedenti/venv/lib/python2.7/site-packages/tastypie_mongoengine/resources.py\", line 423, in dehydrate_resource_type \ n return self._get_type_from_class (type_map, bundle.obj.class) \ n \ n File \"/home/isaias/Projetos/cedenti/venv/lib/python2.7/site-packages/tastypie_mongoengine/resources.py\", line 416, in _get_type_from_class \ n raise KeyError (cls) \ n \ nKeyError: <class'pessoa.models.Pessoa'> \ n "}"
Help me please.