How create a ModelMixin?
See original GitHub issueFirst 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
from datetime import datetime
from typing import Optional
from sqlalchemy.orm import declared_attr
from sqlmodel import Field, Relationship, SQLModel
class ModelMixin:
__name__: str
__config__ = {}
created_at: datetime = Field(default_factory=datetime.utcnow)
update_at: datetime = Field(default=datetime.utcnow)
deleted_at: datetime = Field(default=None, nullable=True)
class Phone(ModelMixin, SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
cellphone: str
is_active: Optional[bool] = Field(default=True)
Description
- Created the ModelMixin with fields
- Created the model Phone
- extends ModelMixin in Phone
- When run code “SQLModel.metadata.create_all(self._engine)” only fields in Phone is created
- The fields created_at, update_at and deleted_at is ignorated
Operating System
macOS
Operating System Details
macos 12.3.1
SQLModel Version
0.0.6
Python Version
Python 3.9.9
Additional Context
No response
Issue Analytics
- State:
- Created a year ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Create Reusable Models with Django and Mixins
Mixins can provide a ton of functionality to not just your database models, but other classes you might have as well. Using them...
Read more >Django Model Mixins: inherit from models.Model or from object?
Mixins inherit from model.Model but are configured as an abstract class. · Because mixins inherit from model.Model , your actual model should not ......
Read more >Use Mixins to create flexible models in Django - DeHaat
Mixin is a concept that aims to solve the problem of multiple inheritances. In brief, it removes the duplicate logic in different classes...
Read more >Using mixins with class-based views - Django documentation
Building up Django's generic class-based views There are also mixins involved in the generic edit views ( FormView , and the model-specific views...
Read more >Model mixins - Django Design Patterns and Best Practices
Model mixins are abstract classes that can be added as a parent class of a model. Python supports multiple inheritances, unlike other languages...
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 Free
Top 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
Indeed, a pure mixin does not appear to work, but perhaps you can do this as a workaround:
That fix works fine for methods and simple attributes, but unfortunately not for model fields, which have to be processed by metaclass machinery. I didn’t realise that until I tried this use case.