Issue: model_to_dict ForeignKeyField problem
Author: serkandaglioglu
State: closed
Created: 2015-09-02 11:33:59

The Model 

```

class User(BaseModel):
    user_id = PrimaryKeyField( db_column="user_id")

    inserter = ForeignKeyField("self",db_column="inserter_id",to_field="user_id")
    updater = ForeignKeyField("self",db_column="updater_id",to_field="user_id", related_name="updated_users")

    email = CharField()
    password = CharField()
    full_name = CharField()
    user_status_id = IntegerField(null=False, default=UserStatusEnum.ACTIVE.value)
    activation_key = CharField()
    mobile = CharField()
    insert_date = DateTimeField(null=True,default=fn.NOW())
    update_date = DateTimeField(null=True)

```

```
Inserter = User.alias()
user = (User
        .select(User, Inserter )
        .join(Inserter, JOIN.LEFT_OUTER, on=(User.inserter==Inserter.user_id))
        .where(User.user_id=userId )
.get()
    )

data["user"] = model_to_dict(user )
```

When I call  `model_to_dict(user )` peewee run sql query for getting updater. I don't want to get updater from database i just only need updater_id. When i call model_to_dict(user , exclude[User.updater]) the dict doesn't have updater field, but i need updater_id not instance of model.   When I call  `model_to_dict(user, recurse=False )` the dict has updater field(its value is updater_id) but it has another problem. Because of `recurse=False` it doesn't include inserter. There is no way that i want to do.


In the current master branch there is a new param for `model_to_dict` called `extra_attrs`. You can use that parameter to indicate you want to include the `updater_id`:

``` python
model_to_dict(User, exclude=[User.updater], extra_attrs=['updater_id'])
```


@coleifer 
Could you add an argument for `model_to_dict` like include_only_sql_seleted_fields=False? If we send `include_only_sql_seleted_fields=True` the dict has only field which selected in sql query. 

For example:

```
Inserter = User.alias()
user = (User
        .select(User, Inserter )
        .join(Inserter, JOIN.LEFT_OUTER, on=(User.inserter==Inserter.user_id))
        .where(User.user_id=userId )
        .get()
    )

data["user"] = model_to_dict(user, include_only_sql_seleted_fields=True)
```

The output will be like this : 

```
{
    "user_id" : 1,
    "full_name" : "Test",
    "email" : "test@gmail.com",
    "user_status_id" : 1,
    "activation_key" : None,
    "mobile" : "0555",
    "insert_date"  : datetime,
    "update_date"  : datetime,
    "updater_id" : 2,
    "inserter" : {
        "user_id" : 2,
        "full_name" : "Test 2",
        "email" : "test2@gmail.com",
        "user_status_id" : 1,
        "activation_key" : None,
        "mobile" : "05553",
        "insert_date"  : datetime,
        "update_date"  : datetime,
        "updater_id" : 2,
        "inserter_id" : 5
   }
}
```


Well, I'd need the actual query in that case so I could know which fields were selected. Something like:

``` python
model_to_dict(user, fields_from_query=query)
```


Don't you keep the query in model object(user) ?


Added `fields_from_query` a93b6268a77039856d0c8ea70e5ded9996c00719.

> Don't you keep the query in model object(user) ?

No.


@coleifer  Thanks.