question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Before sqlmodel I used pydantic modle as input and output Schema . But now i m switched with the sqlmodel but i have some issue , some field(like password) that i don,t want to gave to the user in output schema how it will be restricted:

See original GitHub issue

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the SQLModel documentation, with the integrated search.
  • I already searched in Google “How to X in SQLModel” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to SQLModel but to Pydantic.
  • I already checked if it is not related to SQLModel but to SQLAlchemy.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

class Book(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    title: str
    description: str

Description

Before sqlmodel I used pydantic modle as input and output Schema . But now i m switched with the sqlmodel but i have some issue , some field(like password) that i don,t want to gave to the user in output schema how it will be restricted:

Operating System

Linux

Operating System Details

No response

SQLModel Version

0.0.6

Python Version

3.9.5

Additional Context

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
antontcommented, Apr 23, 2022

@Maypher has it right. There’s a more expanded example in the tutorial, with a base class and what would be here a UserCreate class to define the fields required in a POST call to create such an entity etc. https://sqlmodel.tiangolo.com/tutorial/fastapi/multiple-models/

1reaction
Mayphercommented, Apr 22, 2022

If you mean returning a table without some fields you could do something like this. Let’s say you have a users table

class User(SQLModel, table=True):
    id: Optional[int]
    name: str
    password: str

You want to return the user data without the password (id and name only) so you could do this

class UserRead(SQLModel):
    id: Optional[int]
    name: str  

class User(SQLModel, table=True):
    id: Optional[int]
    name: str
    password: str

Depending on the framework you are using the method might change. I’m assuming you are using FastAPI as your backend. You can use the response_model parameter to filter out unwanted parameters.

@app.get("/user", response_model=UserRead):
    # This is for simplicity. Here you fetch the user from the database
    user = User()
    return user

What this does is it gets the user from the database and when returning, it filters out all the fields that aren’t present in the model you passed to response_model. Here’s more info about the topic

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multiple Models with FastAPI - SQLModel - tiangolo
We have been using the same Hero model to declare the schema of the data we receive in the API, the table model...
Read more >
Using SQLAlchemy ORM, Pydantic and Alembic
One thing that I am running into however, is when I want to add a single column to a table I need to...
Read more >
SQLModel: SQL DBs based on Python type hints. The ...
SQLModel is a library for interacting with SQL DBs, based on Python type hints. Each model is both a Pydantic and SQLAlchemy model, ......
Read more >
SQLModel: The New ORM for FastAPI and Beyond Transcript
I don't know for sure I'm asking you, but it seems to me, looking in from the outside that SQL model was something...
Read more >
See raw diff
I don't know what I'd use it for, but I'm sure there'd be some kind of use out there. ... you can give...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found