Issue: How to select() the fields I need
Author: AlexHeKa
State: closed
Created: 2015-07-21 06:23:04

I  wanna get Post.post_title,Post.post_content...and also try to get User.user_name which its id == Post.user

I try

``` python
query = Post.select().join(Act).where(Act.id == actId).paginate(actPage, 10) or
query = Post.select(Post.post_title,Post.post_content).join(Act).where(Act.id == actId).paginate(actPage, 10) 
```

But it returns all the fields in User,Act,Post,so how to select() the fields I need? Thanks

Here is my model

``` python

class User(BaseModel):
    user_name = CharField(max_length=30,unique=True) 
    user_email = CharField(max_length=60,unique=True) 
    user_pass = CharField(max_length=60) 
    user_salt = CharField(max_length=128)
    user_registeredTime = DateTimeField()
    user_portrait_url = CharField(max_length=400,default="http:/xxx.com")
    user_point = IntegerField(default=0)
    user_message = CharField(max_length=60,null=True)  


class Act(BaseModel):
    user = ForeignKeyField(User, related_name='users_id')  
    act_title = CharField(max_length=30)
    act_content = CharField(max_length=420)
    act_thumb_url = CharField(max_length=400)
    act_type = IntegerField(default=0)
    act_star = IntegerField(default=0)
    act_status = IntegerField(default=0) 
    act_alias = CharField(max_length=30)
    act_url = CharField(max_length=400)
    act_date_gmt = DateTimeField()


class Post(BaseModel):
    act = ForeignKeyField(Act, related_name='acts_id')
    user = ForeignKeyField(User, related_name='users_id2') 
    post_title = CharField(max_length=30)
    post_content = CharField(max_length=180)
    post_thumb_url = CharField(max_length=400)
    post_date_gmt = DoubleField()
    post_likes_count = IntegerField()
    post_comments_count = IntegerField()
    post_views_count = IntegerField()
```


Cool models bro, what's your question?


Maybe `query.tuples()` or `query.dicts()` are exactly what you looking for?


You want something like:

``` python
Post.select(Post.post_title, Post.post_content, User.username).join(User)
```

Then you can iterate through the results of the query and access `post.post_title`, `post.post_content`, and `post.user.username`.


For future reference, questions like this belong on StackOverflow rather than the peewee bug tracker.


When I used

``` python
query = Post.select(Post.post_title, Post.post_content).join(Act).where(Act.id == actId).paginate(actPage, 15)
query_dataList = []
for query_line in query:
    query_data = model_to_dict(query_line)
    query_dataList.append(query_data) 
    json_data = json.dumps(query_dataList)
```

then  converted it to json,I got this

``` json
[{"post_views_count": null, "post_title": "\u4e0d\u9519", "post_comments_count": null, "post_likes_count": null...
```

It seems like the fields I didn't include in the "select()" return null,and I only want the selected fields.I'm not sure is't something wrong with my code or it is designed like this and I will ask in StackOverflow next time,Thanks.


function `model_to_dict` serialises **all** fields from model (wholly). You should use `model_to_dict(query_line, only=(Post.post_title, Post.post_content))`.

Or as I said before use `query.dicts()`


Yes, you should read the documentation on `model_to_dict`: http://docs.peewee-orm.com/en/latest/peewee/playhouse.html#model_to_dict

It will serialize everything unless you specify `only`, which in your case seems to be what you want.


@klen  @coleifer  Thanks bro.