Abstract Model for common fields
See original GitHub issueProblem
I think common fields can be excluded from models and be in one single abstract model, also I find it can be a little bit redundant to have to write them over and over again.
Suggested solution
Have an abstract modifier before the model to exclude it in the database and all the models that extends it can have all the predefined fields.
Alternatives
Abstract modifler
abstract model BaseFields {
id String @id @default(uuid()) @db.VarChar(36)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
Implementation
model User extends BaseFields {
username String @unique @db.VarChar(20)
password String @unique
Post Post[]
@@map("users")
}
model Post extends BaseFields {
title String
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId String
@@map("posts")
}
Result
model User extends BaseFields{
id String @id @default(uuid()) @db.VarChar(36) // auto generated filed or ingected?
createdAt DateTime @default(now()) // auto generated filed or ingected?
updatedAt DateTime @updatedAt // auto generated filed or ingected?
username String @unique @db.VarChar(20)
password String @unique
Post Post[]
@@map("users")
}
model Post extends BaseFields {
id String @id @default(uuid()) @db.VarChar(36) // auto generated filed or ingected?
createdAt DateTime @default(now()) // auto generated filed or ingected?
updatedAt DateTime @updatedAt // auto generated filed or ingected?
title String
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId String
@@map("posts")
}
Additional context
I’m open to all comments, so please leave something useful. This is my first post here, please be kind. If this issue is duplicated, mention it.
Issue Analytics
- State:
- Created a year ago
- Reactions:28
- Comments:5
Top Results From Across the Web
Using abstract models in Django - DEV Community
All you have to do is create a base model with all the common fields and set abstract = True in the meta...
Read more >Python Snippets: Querying Abstract Models
Abstract Base Classes. Abstract models allow for common fields to be placed in a parent class, but tables are only created for derived...
Read more >How to create Abstract Model Class in Django? - GeeksforGeeks
So here I create a model common and put the common fields in that model. Put abstract = True in the Meta class....
Read more >How to create an abstract model class in Django - Dev Genius
“Abstract base classes are useful when you want to put some common information into a number of other models. You write your base...
Read more >How to create an admin mixin for common/abstract model in ...
The TenantAwareModel is an abstract model that allows you to avoid repeating the common fields in the definition of others models, ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
+1 Would love to see this feature implemented!
+1 for this feature. Thanks!