python - Print peewee model as json with foreign_keys instead of related model - Stack Overflow

I'm currently exploring peewee ORM and in order to make some tests I've defined two simple da

I'm currently exploring peewee ORM and in order to make some tests I've defined two simple databases with three tables as follow

I then defined the peewee models into a single file

from playhouse.shortcuts import model_to_dict
import peewee as pw
import json

public_database = pw.MySQLDatabase(None)
private_database = pw.MySQLDatabase(None)

class CommonModel(pw.Model):

    def json(self, recurse=False):
        return model_to_dict(self, recurse=recurse)

    def __str__(self):
        return json.dumps(self.json())

class PublicDatabaseModel(CommonModel):
    class Meta:
        database = public_database

class PrivateDatabaseModel(CommonModel):
    class Meta:
        database = private_database

class User(PrivateDatabaseModel):
    class Meta:
        db_table = "users"
    
    id = pw.AutoField(primary_key=True)
    firstname = pw.CharField()
    lastname = pw.CharField()
    email = pw.CharField()

class Character(PublicDatabaseModel):
    class Meta:
        db_table = "characters"

    id = pw.AutoField(primary_key=True)
    name = pw.CharField()

    user = pw.ForeignKeyField(User)

class Item(PublicDatabaseModel):
    class Meta:
        db_table = "items"

    id = pw.AutoField(primary_key=True)
    name = pw.CharField()

    character = pw.ForeignKeyField(Character)

I can then define database connections into a testing file

import peewee_models as models

models.private_database.init(
    "test_database_private",
    host="localhost",
    user="root",
    password="password",
    port=3306
)
models.public_database.init(
    "test_database_public",
    host="localhost",
    user="root",
    password="password",
    port=3306
)

characters = models.Character.select()
for character in characters:
    print("-------------------")
    print(f"Character : {character}")
    print(f"Character.id : {character.id}")
    print(f"Character.name : {character.name}")
    print(f"Character.user_id : {character.user_id}")
    print(f"Character.user : {character.user}")

The issue I have is that I can't find a way to render models as JSON with the foreign_key name as a key, the key name is always the name of the associated relationship

-------------------
Character : {"id": 1, "name": "User1_Char1", "user": 1}
Character.id : 1
Character.name : User1_Char1
Character.user_id : 1
Character.user : {"id": 1, "firstname": "user1_firstname", "lastname": "user1_lastname", "email": "[email protected]"}
-------------------
Character : {"id": 2, "name": "User1_Char2", "user": 1}
Character.id : 2
Character.name : User1_Char2
Character.user_id : 1
Character.user : {"id": 1, "firstname": "user1_firstname", "lastname": "user1_lastname", "email": "[email protected]"}

For example I'd like the Character class JSON to be {"id": 2, "name": "User1_Char2", "user_id": 1} instead of {"id": 2, "name": "User1_Char2", "user": 1} but I can't find a way to achieve it.

I tried to change several ForeignKeyField class parameters such as object_id_name and related_name but nothing seems to change anything, even though the user_id attribute does exist in the class instance.

Does anyone know how to have the foreign_key name instead of the relationship name as JSON key ?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743780730a4505748.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信