Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7667033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:59:36+00:00 2026-05-31T14:59:36+00:00

Is Django or maybe POSTGRESQL losing information about primary keys and foreign keys when

  • 0

Is Django or maybe POSTGRESQL losing information about primary keys and foreign keys when you create a view which relates to a view which relates to a table, which has primary and foreign keys?

I have a View-A (all 3 fields are Foreign Keys) and that view gets 2 fields from a View-B. The View-B gets its fields from a table-C. table-C has primary key and foreign key.

So when i access View-A with my django model, how do i treat those fields? I know they are foreign keys, but any kind of filter results in a empty Queryset.
if i use something like

myview = viewA.objects.using(db).all()  # getting all the data
myview2= viewA.objects.using(db).all()[:5] # getting 5 objects

.

class viewA(models.Model):
class Meta:
    db_table = "viewA"

x = models.ForeignKey(x, primary_key=True)
y = models.ForeignKey(y)
z = models.ForeignKey(z)

The problem is that i can not filter!

response=viewA.objects.using(db).filter(y_id=1)  ERROR:= FieldError

Behind all those FK, there are integer/bigint fields.

Edit:
Since this are INNER JOINS i would like to access not only the fields from ViewA, but also from ViewB. x,y,z are from type ViewB. Maybe use select_related()?

So any clues if Django or postgres lose information about keys in views which relate to other views?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T14:59:37+00:00Added an answer on May 31, 2026 at 2:59 pm

    See my two comments; however, to answer your specific “query”.

    If you go to the docs, and see this paragraph:

    https://docs.djangoproject.com/en/dev/ref/models/fields/#database-representation

    You will note that foreign keys, by default, have the _id appended to their names. Now, there are occasions in which you need to access the column name directly and it’s a good idea to be aware of the column as a “bigger picture” sort of thing, but at as far as Model API is concerned, you should, when doing something against a foreign key, use the attribute name given in the model instance.

    Edit from your comment:

    If you want to drill down and filter against some field in the foreignkey object, you just do y__fieldname = somevalue

    Note that fieldname must be in the y object.

    If you are getting back and empty queryset, this is because nothing in that column matched the value you gave it.

    To test this, create a queryset directly on the “y” object and then try to do y.objects.fitler(fieldname=somevalue)

    if you still get back and empty queryset, you know that value doesn’t exist. Furthermore, you can look into the database and try a raw query in pgadminIII if you have that set up.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Django view points to a function, which can be a problem if you want
Django has a built in serialization functionality which allows you to serialize any query
All, I'd like to check Django out and maybe hack together some web pages
Introspection tells me that django fields have a hidden_widget attribute. Maybe its not this
Django comes with CSRF protection middleware , which generates a unique per-session token for
Django has a number of open source projects that tackle one of the framework's
django's manager docs has a paragraph overwritten with DO NOT FILTER AWAY ANY RESULTS
Django has various numeric fields available for use in models, e.g. DecimalField and PositiveIntegerField
I'm new to Django. Today I created some Django custom tags which is not
Using Django with a PostgreSQL (8.x) backend, I have a model where I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.