marshmallow-code / marshmallow-code/marshmallow
Extract value from a List of object
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 7.2k
- Forks
- 738
- Avg merge
- 1d 23h
- Merged PRs (30d)
- 7
Description
Hello,
I have the following structure :
```
class Entity(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255), nullable=False)
entity_user = db.Table('entity_user',
db.Column('entity_id', db.Integer, db.ForeignKey('entity.id')),
db.Column('user_id', db.Integer, db.ForeignKey('user.id'))
)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
entities = db.relationship('Entity', secondary=entity_user,
lazy='subquery',
backref=db.backref('users', lazy=True))
```
Inside my user schema, I would like to dump the ID of the entities he owns. The most easiest way is via Nested fields but I can't use them (I have either a circular import issue if I call the Schema class directly or an issue with an another package if I specify the Schema class via its name string)
I have be looking for few hours and I have found several solutions but none which is totally working for me.
**Pluck**
```
class UserSchema(Schema):
entitiesId = fields.Pluck("EntitySchema", "id", many=True, attribute='entities')
```
This method successfully dumps the data, but I don't know how I can load them.
With this json :
```
{
"entitiesId": [
1,2
],
"name": "string",
}
```
or this one :
```
{
"entitiesId": [
{"id" : 1}, {"id": 2}
],
"email": "string",
}
```
I have this error :
```
{
"schema_errors": {
"entitiesId": {
"0": {
"id": [
"Unknown field."
]
},
"1": {
"id": [
"Unknown field."
]
}
}
}
}
```
**Direct attribute**
I have also tried this schema :
```
class UserSchema(Schema):
entitiesId = fields.Integer(many=True, attribute='entities.id')
```
but nothing appears when I dump.
I didn't find a solution with pre_dump or post_dump.
Do you have an idea how I can achieve this ?
Thank you,
RomD
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the UserSchema examples in the issue, especially fields.Pluck and the direct fields.Integer declaration, and reproduce both dump and load cases using the shown JSON. Trace how the entities relationship is handled during serialization and deserialization. Done means entity IDs can be dumped and loaded without the reported unknown-field or missing-output errors, with coverage for both input shapes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100