Friday, May 29, 2020

Re: filter on multiple tables

Hi,

whenever I'm trying to filter as per your convention I'm getting some error like FieldError:
Eg. profile = Profile.objects.filter(education__profile__institute_name="IIT KGP")

of course, I'm getting this error because Profile is working here as a Foreign Key, in Education table.

actually I want to fetch all the users those are from the same Institution or Specialization or location.


Thank you for your response and time.

Regards,
Soumen


‪On Fri, May 29, 2020 at 9:56 AM ‫אורי‬‎ <uri@speedy.net> wrote:‬
Hi Soumen,

You can filter on another table by using <table_name>__<field>, for example:

User.objects.filter(profile__profiles__institute_name="Example")

Or:

User.objects.filter(profile__profiles__institute_name__in=["Example 1", "Example 2"])


On Thu, May 28, 2020 at 11:27 PM Soumen Khatua <soumenkhatua258@gmail.com> wrote:
Actually I want to filter all users those are matched with currently logged in users based on location or education details like maybe institute_name or specialization. Is it possible to search in multiple tables?!
I almost spent 2 days but still I stuck in the position.
Any help would be appreciated.

Thank You

Regards,
Soumen


class Location(models.Model):
    name = models.CharField(max_length = 15)

class Profile(models.Model):
    user = models.OneToOneField(
        settings.AUTH_USER_MODEL,
        on_delete = models.CASCADE,
        related_name="profile"
    )
    location = models.ManyToManyField(Location,related_name = 'user_locations')

class Education(models.Model):
    profile = models.ForeignKey(Profile, on_delete=models.CASCADE,related_name="profiles")
    institute_name = models.CharField(max_length = 250)
    specialization = models.CharField(max_length = 100)


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPUw6WZqX9oqutYfGJqAwCswY_00VfTmf624ypypAr6saS61Nw%40mail.gmail.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CABD5YeFfHWDEkTFwMFocApuG3ejYY5QjqHHd_86wNNc-JoWP4g%40mail.gmail.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPUw6WazA6k1MHjUWiMWvEPU9HnitRK7bBzq-aQVOrP9w84o-Q%40mail.gmail.com.

No comments:

Post a Comment