Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relation fields do not support nested lookups #167

Open
BuckinghamIO opened this issue Oct 17, 2015 · 2 comments
Open

Relation fields do not support nested lookups #167

BuckinghamIO opened this issue Oct 17, 2015 · 2 comments

Comments

@BuckinghamIO
Copy link

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
Copy link
Author

Its this line that trigger the error:

    channels = filter_channels_by_model(all_model_channels, obj)

Which is inside the created modelpubrouter

@BuckinghamIO
Copy link
Author

Anyone able to shine some light on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant