hagsteel / swampdragon

swampdragon
Other
557 stars 74 forks source link

Relation fields do not support nested lookups #167

Open BuckinghamIO opened 8 years ago

BuckinghamIO commented 8 years ago

I have two models:

class Conversation(models.Model):
serializer_class = ConversationSerializer

created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)

def __unicode__(self):
    return '%s' % self.id

class Message(models.Model):
serializer_class = MessageSerializer

conver = models.ForeignKey(Conversation)
#participant = models.ForeignKey(Participant, blank=True, null=True)
message = models.CharField(max_length=1000)
sent = models.DateTimeField(auto_now_add=True)

def __unicode__(self):
    return '%s' % self.message

as you can see the message model is related to the conversation model by a foreign key, however when I go to save a message I get a errored raised here:

def get_lookup_constraint(self, constraint_class, alias, targets, sources, lookups,
                          raw_value):
    from django.db.models.sql.where import SubqueryConstraint, AND, OR
    root_constraint = constraint_class()
    assert len(targets) == len(sources)
    if len(lookups) > 1:
        raise exceptions.FieldError('Relation fields do not support nested lookups')
    lookup_type = lookups[0]

The router gets this data on the create call:

{u'message': u'test', u'conver_id': 11}

and the router is set to handle it like so:

class MessageRouter(ModelPubRouter):
valid_verbs = ModelPubRouter.valid_verbs

route_name = 'message-route'
model = Message
serializer_class = MessageSerializer
include_related = []

def get_initial(self, verb, **kwargs):
    initial = {}
    print(kwargs)
    if verb == 'create':
        convo = Conversation.objects.get(id=kwargs['conver_id'])
        initial = {'sent':timezone.now(), 'conver':convo}
    elif verb == 'update':
        initial = {'user': self.connection.user}

    return initial

def get_object(self, **kwargs):
    return self.model.objects.get(pk=kwargs['id'])

def get_query_set(self, **kwargs):
    print(kwargs)
    return self.model.objects.filter(**kwargs)

as you can see in the get_initial it takes the id and turns it into a obj to pass to the create which is when I hit the error.

I thought at first it was because of SelfPublish but this happens on any create call if I try and save with a foreign key...

Please please please help me cause I need to get this done.

BuckinghamIO commented 8 years ago

Its this line that trigger the error:

    channels = filter_channels_by_model(all_model_channels, obj)

Which is inside the created modelpubrouter

BuckinghamIO commented 8 years ago

Anyone able to shine some light on this?