Django Queries

20 views
Skip to first unread message

Gerald Brown

unread,
May 12, 2018, 2:15:42 AM5/12/18
to Django users
Greetings:

I have just started to use Django Queries and I am having some problems.

When i use the query: "p = People.objects.all()" & then "p" it shows
just the lastname & firstname fields even though there are many more
fields in the table.

I thought ALL means ALL, ALL fields & ALL records!!!

Only 2 records so far in the DB and it shows both of them so ALL RECORDS
is working.

Another table I have has a date field and a ForeignKey to the people
table plus some additional fields.  The query returns the date and the
firstname and lastname from the people table ONLY.

It looks like it is only showing the first two fields in the table.

Any ideas on how I can get it to show ALL fields?

Thanks.

Joseph Mutumi

unread,
May 12, 2018, 4:35:15 AM5/12/18
to django-users@googlegroups com
Hello,

That's strange what does your people model look like? Notice it
will only populate the fields you have defined in the model not 
the columns of the table in the database.

Did you run migrations after adding new fields to the model? 
$ python manage.py migrate

Kind regards



--
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 post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7e90ad4d-bffe-5690-2559-ec4655fc79b6%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

Mark Phillips

unread,
May 12, 2018, 5:34:50 AM5/12/18
to django users
Please provide the exact output you are seeing. It may be that the output has been truncated, which is correct behavior.

For example, when I run this query on the command line for my model Document,

>>> Document.objects.all()
<QuerySet [<Document: a new title 2>, <Document: adfadfasdf>, <Document: gggggg>, <Document: the title>]>

I get a QuerySet with 4 entries, as there are 4 rows/records in the Document table. All the fields are there, just not shown. The __str__ method in the Document model returns the title of the document, which is what is shown above for each Document in the QuerySet. I am guessing but your People table probably defines the __str__ method to return the first and last name.

I can look at one of the returned rows like this:

>>> d=Document.objects.all()
>>> d[0]
<Document: a new title 2>
>>> d[0].document_id
2
>>> d[0].title
'a new title 2'
>>> d[0].description
'a new description 2'
>>> d[0].document_state
1
>>> d[0].storage_file_name
<FieldFile: c4b592c8-662c-437a-819e-c36df8867a9c.png>
>>> 

title, document_id, description, and storage_file_name are all fields defined in the model for Document (along with others).

Read the django documentation about models and queries for more information.

Mark

Gerald Brown

unread,
May 12, 2018, 5:41:53 AM5/12/18
to django...@googlegroups.com

Thanks for your reply.

My model consists of the following:

id, first_name, last_name, middle_initial, contact_num, address, town(FK to Town table), date_of_birth, occupation(FK to Occupation table), email & gender. The Django query returns ONLY first_name & last_name.

I run migrations after any changes to any models. The database has the same fields that are shown.

BTW, the query is being run after running ./manage.py shell.

To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Gerald Brown

unread,
May 12, 2018, 6:13:28 AM5/12/18
to django...@googlegroups.com

I was getting the same as you are as I had just first_name & last_name set in the "def __str__(self)".  I added a couple of more fields to that statement but they are still not showing up even after running a makemigration which says "No Change"

I tried "p.address" but it gave me an error message, (which I don't remember) as I did not know about the "[]"s.  Now if I do "p.[0].address" it gives me the address. Also if I do "p[0].town" it show me the town name, which is in another table and is a FK to that table.

Like I said in my original message, I am just starting to use Django Queries so there is a lot to learn so I thank you for your assistance.

To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Joseph Mutumi

unread,
May 12, 2018, 6:42:09 AM5/12/18
to django-users@googlegroups com
Hello,

You need to clearly understand Python objects because the ORM maps fields 
directly to that.


So if you print out the object as a string i.e.

print people

OR

person = People.objects.get(pk=1)
print person

The string returned by the __str__ or __unicode__ method is what is displayed.
To access other columns you need to access the attributes like: person.address
or person.occupation. Through foreign keys too e.g. person.town.name.

Read through the docs and follow those examples first. You'll easily understand 
thereafter.

Kind regards

--
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 post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages