0

I am using a conditional statement like this in my code:

if not profile.client.user.id == 3:

Somehow that gives me Exception: Client matching query does not exist. exception. This is just a conditional operator, so I am not sure why am I getting this exception. Does anyone have any clue what could be going wrong?

UserProfile Model

class UserProfile(models.Model):
    # This field is required.
    user = models.OneToOneField(User)
    client = models.ForeignKey(Client,null=True)

Client Model

class Client(models.Model):
    user = models.ForeignKey(AUTH_USER_MODEL, related_name='oauth2_client',
        blank=True, null=True)

User Model

This is a standard django user model

Jonathan
  • 2,728
  • 10
  • 43
  • 73
  • What's the code for `profile.client.user.id`? Is that a data descriptor? – Eithos Feb 10 '15 at 07:02
  • @Eithos profile is a userprofile model and client is a foreign key, client has user as foreign key and user has id – Jonathan Feb 10 '15 at 07:05
  • 1
    I just don't see how we can help you with the little bit of code you've given us. There is nothing mysterious about the way the conditional operator works; it's just not what's causing your error. The error is somewhere in the code of `profile.client.user.id`. We don't even know what kind of object it is, if the `id` attribute is a descriptor (so the error could be coming from the `__get__` if that's what it is) or from `__eq__`, if it's been defined. We just have no way to figure this out without having access to that information. – Eithos Feb 10 '15 at 07:13
  • Just to add to that quickly, I hadn't noticed the _django_ tag until now. If this `profile.client.user.id` is somehow familiar to people who've used it, by all means ignore what I've said if it doesn't apply. I still have trouble seeing how you'll get help without more information, though. – Eithos Feb 10 '15 at 07:17
  • @Eithos Please check the edits – Jonathan Feb 10 '15 at 07:18

2 Answers2

0

Try to use pk instead of id

if not profile.client.user.pk == 3:

More their: What's the difference between Model.id and Model.pk in django?

Community
  • 1
  • 1
adrien
  • 126
  • 1
  • 14
0

This means that you database is broken - profile.client field points to the Client that does not exist. You can check it with the following query:

client = Client.objects.get(pk=profile.client_id)

If such client exists then this query will be executed normally. If client does not exist then the exception will be raised.

catavaran
  • 44,703
  • 8
  • 98
  • 85