0

I have this model

class UserInfo(models.Model) :
    userNumber = models.CharField(max_length=15)
    userPin = models.CharField(max_length=7, unique=True)


    def __str__(self) :
        return str(self.userPin)


class UserFollows(models.Model) :
    following = models.ForeignKey(UserInfo, on_delete=models.CASCADE, related_name='UserFollows.following+')
    followers = models.ForeignKey(UserInfo, on_delete=models.CASCADE, related_name='UserFollows.followers+')

I need inner join for userPin. This command try error

UserInfo.objects.filter(userPin__UserFollows__followers= '****')

Unsupported lookup 'UserFollows' for CharField or join on the field not permitted.

Xaliq
  • 9
  • 1

1 Answers1

0

In your model userPin is a CharField, not a FK. You can join UserInfo and UserFollows via reverse relation and filter it with userPin:

UserInfo.objects.filter(user_follows__followers__userPin='value')
Kyryl Havrylenko
  • 674
  • 4
  • 11
  • now try this error: "Choices are: %s" % (name, ", ".join(available))) django.core.exceptions.FieldError: Cannot resolve keyword 'userFollows' into field. Choices are: id, userNumber, userPin [28/Jan/2019 16:26:04] "GET /api/following/?pin=1A HTTP/1.1" 500 105921 – Xaliq Jan 28 '19 at 12:26
  • `UserInfo` in your code does not have `userFollows` field so of course Django cannot resolve that keyword. If you want to alter filter query, read [docs on how to follow relations](https://docs.djangoproject.com/en/2.1/topics/db/queries/#lookups-that-span-relationships). If you want to access reverse relation directly on an instance see [this question](https://stackoverflow.com/questions/15306897/django-reverse-lookup-of-foreign-keys). – Kyryl Havrylenko Jan 28 '19 at 13:04