Everything works except query does not populate models

205 views
Skip to first unread message

boo...@hotmail.com

unread,
Sep 23, 2014, 9:35:42 PM9/23/14
to qdj...@googlegroups.com
I've followed the examples in the documentation.  Both exactly with the User class and tried to create my own model.  I am able to register and insert rows into the database using the code from the documentation.  When I create a QDjangoQuerySet (both with and without filtering), I can see it is getting rows from the database - users.count() shows this.  However, when I iterate over the users, using both for and foreach methods, the resulting objects are null.  Am I missing a step?

I followed all instructions to configure and register the models.  Like I said, I am able to do everything else through QDjango like delete all rows, create tables, and even selects return the count of rows I'm expecting.  I am using Sqlite3 on Windows 8.1 and Qt 5.3.1

Example of what is happening
QDjangoQuerySet<User> users;
qDebug() << "Count of all users: " << users.count();  // prints the number of rows I'm expecting
foreach(const User& user, users){
  qDebug() << "Found user: " << user.username() << ", ";  // user is null, invoking username() crashes
}

Thanks

otop...@gmail.com

unread,
Sep 26, 2014, 6:42:22 AM9/26/14
to qdj...@googlegroups.com
Try to call .size() and then foreach.
If I understand that correctly .count() makes just another select without populating queryset.
size() should trigger the population..
But I dont get it .. it should work without calling anything.
You can check QDjango::database().isOpen() before foreach .. I dont know.

I am just another user of thins library - no expert..

boohaus

unread,
Sep 26, 2014, 3:10:44 PM9/26/14
to qdj...@googlegroups.com
Hi otop,

Thanks for your reply.  I tried what you suggested.  

- count() returns the number of objects I expect out of the table.  When I add rows to the table, count() is still accurate.
- size() always returns -1
- using foreach causes seg fault on call to user.username() because model instances are not populated.
- using "for(int i = 0; i < users.size(); ++i)" does not loop at all because users.size() is always -1.  If I use user.count() it loops, but model instances are still not populated.
- QDjango::database().isOpen() returns true.

I really want this to work.  I like everything else about this project.  Easy and straightforward to set up and use.  Hopefully it's a simple configuration error on my part.

Thanks again for your help.

Jeremy Lainé

unread,
Sep 26, 2014, 3:55:04 PM9/26/14
to qdj...@googlegroups.com
Hi,

On 09/26/2014 09:10 PM, boohaus wrote:
> Hi otop,
>
> Thanks for your reply. I tried what you suggested.
>
> - count() returns the number of objects I expect out of the table. When I add rows to
> the table, count() is still accurate.
> - size() always returns -1
> - using foreach causes seg fault on call to user.username() because model instances are
> not populated.
> - using "for(int i = 0; i < users.size(); ++i)" does not loop at all because
> users.size() is always -1. If I use user.count() it loops, but model instances are
> still not populated.
> - QDjango::database().isOpen() returns true.
>
> I really want this to work. I like everything else about this project. Easy and
> straightforward to set up and use. Hopefully it's a simple configuration error on my part.
>


Try adding QDjango::setDebugEnabled(true) at the very start of your program and post the
resulting output. You are most likely hitting an SQL error (not that it excuses the
crash), I'd like to know what it is.

Cheers,
Jeremy

boohaus

unread,
Sep 26, 2014, 5:02:16 PM9/26/14
to qdj...@googlegroups.com
Hi Jeremy,

Thanks for the debug feature.  The output is:

SQL query "SELECT COUNT(*) FROM "user""

SQL query "SELECT "user"."id", "user"."username", "user"."password" FROM "user""

SQL error QSqlError("", "Unable to fetch row", "No query")

And the reason the query failed is because I didn't define the field "id" in the table that it was looking for.  At one time I had issues setting the primary key with Q_CLASSINFO so I skipped it.I'll have to review that part again.

Thank you!  It looks like you regularly update the project.  Keep it up!

Scott
Reply all
Reply to author
Forward
0 new messages