Skip to content

Partial Types

Prisma Client Python exposes an interface for creating partial models at generation time based off of schema-defined models, see partial-types for more information.

Each model defined in the schema can have partial types generated based off of it. The API looks like this:

class Model:
  @staticmethod
  def create_partial(
      name: str,
      include: Optional[Iterable['{{ model }}Keys']] = None,
      exclude: Optional[Iterable['{{ model }}Keys']] = None,
      required: Optional[Iterable['{{ model }}Keys']] = None,
      optional: Optional[Iterable['{{ model }}Keys']] = None,
  ) -> None:
      ...
where {{ model }}Keys is Literal type containing all the fields of the model.

For example:

model User {
  id      String   @default(cuid()) @id
  name    String
  posts   Post[]
  email   String?
}
UserKeys = Literal['id', 'name', 'posts', 'email']

Reference

name
The name given to the generated partial type, must be unique.
include
An iterable of field names that will be available in the generated model. Cannot be used at the same time as exclude.
exclude
An iterable of field names that will not be available in the generated model. Cannot be used at the same time as include.
required
An iterable of field names that will not be marked as optional in the generated model.
optional
An iterable of field names that will be marked as optional in the generated model.

Examples

All examples use the following models

model User {
  id      String   @default(cuid()) @id
  name    String
  profile Profile?
  email   String?
}

model Profile {
  id       Int    @id @default(autoincrement())
  user     User   @relation(fields: [user_id], references: [id])
  user_id  String
  bio      String
}
class User:
  id: str
  name: str
  profile: Optional['Profile']
  email: Optional[str]

class Profile:
  id: int
  user: Optional['User']
  user_id: str
  bio: str

Include

from prisma.models import User
User.create_partial('UserOnlyName', include={'name'})
User.create_partial('UserOnlyNameAndEmail', include={'name', 'email'})
class UserOnlyName:
  name: str

class UserOnlyNameAndEmail:
  name: str
  email: Optional[str]

Exclude

from prisma.models import User
User.create_partial('UserWithoutProfile', exclude=['profile'])
class UserWithoutProfile:
  id: str
  name: str
  email: Optional[str]

Optional

from prisma.models import User
User.create_partial('UserOptionalName', optional={'name'})
class UserOptionalName:
  id: str
  name: Optional[str]
  profile: Optional['Profile']
  email: Optional[str]

Required

from prisma.models import User
User.create_partial('UserRequiredEmail', required={'email'})
class UserRequiredEmail:
  id: str
  name: str
  profile: Optional['Profile']
  email: str