dymmond / mongoz Goto Github PK
View Code? Open in Web Editor NEWODM with Pydantic made it simple
Home Page: https://mongoz.dymmond.com
License: MIT License
ODM with Pydantic made it simple
Home Page: https://mongoz.dymmond.com
License: MIT License
Hello,
I have a Model as:
indexes = [
Index(keys=[("year", Order.DESCENDING), ("genre", IndexType.HASHED)]),
]
class AnotherMovie(Document):
name: str = mongoz.String()
email: str = mongoz.Email(index=True, unique=True)
year: int = mongoz.Integer()
uuid: Optional[ObjectId] = mongoz.ObjectId(null=True)
class Meta:
registry = client
indexes = indexes
database = "test_db"
autogenerate_index = True
When I create a duplicate record in this model, it allows me to do so, unless I use the await AnotherMovie.create_indexes()
method.
And if any model does not have indexes, calling the await AnotherMovie.create_indexes()
method returns an error as well.
Thank you.
Can anyone give me a solution for how to differentiate a data models on the basis of database. I am trying to access these database on the basis custom parameters add in the meta class but it is not working?
Hello,
I have the two relation i.e. Employee and Supervisors.
Employee document has the the Supervisor as the embedded document
class Employee(Document):
full_name: str = mongoz.String
employee_code: str = mongoz.String
date_of_joining: datetime.datetime = mongoz.DateTime(null=True)
e_mail: str = mongoz.String(null=True)
mobile_no: str = mongoz.String(null=True)
supervisors: List[EmployeeSupervisor] = mongoz.Array(EmployeeSupervisor)
class EmployeeSupervisor(EmbeddedDocument):
supervisor: ObjectId = mongoz.ObjectId # Object id of employee document.
effective_from: datetime.datetime = mongoz.DateTime
effective_to: datetime.datetime = mongoz.DateTime
Error as:
Traceback (most recent call last):
File "/usr/lib/python3.10/multiprocessing/process.py", line 314, in _bootstrap
self.run()
File "/usr/lib/python3.10/multiprocessing/process.py", line 108, in run
self._target(*self._args, **self._kwargs)
File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/uvicorn/_subprocess.py", line 78, in subprocess_started
target(sockets=sockets)
File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/uvicorn/server.py", line 65, in run
return asyncio.run(self.serve(sockets=sockets))
File "/usr/lib/python3.10/asyncio/runners.py", line 44, in run
return loop.run_until_complete(main)
File "/usr/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
return future.result()
File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/uvicorn/server.py", line 69, in serve
await self._serve(sockets)
File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/uvicorn/server.py", line 76, in _serve
config.load()
File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/uvicorn/config.py", line 433, in load
self.loaded_app = import_from_string(self.app)
File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/uvicorn/importer.py", line 19, in import_from_string
module = importlib.import_module(module_str)
File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/home/harshaliz/Desktop/Projects/My_proj/trunk/my_apps/main.py", line 10, in <module>
from playmity.apps.user_management.views import get_database_collection
File "/home/harshaliz/Desktop/Projects/My_proj/trunk/my_apps/apps/__init__.py", line 5, in <module>
from playmity.apps import user_management
File "/home/harshaliz/Desktop/Projects/My_proj/trunk/my_apps/apps/user_management/__init__.py", line 4, in <module>
from .urls import *
File "/home/harshaliz/Desktop/Projects/My_proj/trunk/my_apps/apps/user_management/urls.py", line 22, in <module>
from .views import (
File "/home/harshaliz/Desktop/Projects/My_proj/trunk/my_apps/apps/user_management/views/__init__.py", line 9, in <module>
from .authz import *
File "/home/harshaliz/Desktop/Projects/My_proj/trunk/my_apps/apps/user_management/views/authz.py", line 31, in <module>
from playmity.apps.user_management.models import (
File "/home/harshaliz/Desktop/Projects/My_proj/trunk/my_apps/apps/user_management/models/__init__.py", line 7, in <module>
from .employee import Employee, EmployeeBranch, EmployeeRole, EmployeeGrade, \
File "/home/harshaliz/Desktop/Projects/My_proj/trunk/my_apps/apps/user_management/models/employee.py", line 131, in <module>
class EmployeeSupervisor(BaseEmbeddedDocument):
File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/mongoz/core/db/documents/metaclasses.py", line 420, in __new__
new_class = super().__new__(cls, name, bases, attrs)
File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/pydantic/_internal/_model_construction.py", line 183, in __new__
complete_model_class(
File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/pydantic/_internal/_model_construction.py", line 517, in complete_model_class
schema = cls.__get_pydantic_core_schema__(cls, handler)
File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/pydantic/main.py", line 584, in __get_pydantic_core_schema__
return __handler(__source)
File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/pydantic/_internal/_schema_generation_shared.py", line 82, in __call__
schema = self._handler(__source_type)
File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 490, in generate_schema
schema = self._generate_schema(obj)
File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 721, in _generate_schema
schema = self._generate_schema_inner(obj)
File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 742, in _generate_schema_inner
return self._model_schema(obj)
File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 563, in _model_schema
{k: self._generate_md_field_schema(k, v, decorators) for k, v in fields.items()},
File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 563, in <dictcomp>
{k: self._generate_md_field_schema(k, v, decorators) for k, v in fields.items()},
File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 906, in _generate_md_field_schema
common_field = self._common_field_schema(name, field_info, decorators)
File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 1071, in _common_field_schema
schema = self._apply_annotations(
File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 1765, in _apply_annotations
schema = get_inner_schema(source_type)
File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/pydantic/_internal/_schema_generation_shared.py", line 82, in __call__
schema = self._handler(__source_type)
File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 1746, in inner_handler
schema = self._generate_schema(obj)
File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 721, in _generate_schema
schema = self._generate_schema_inner(obj)
File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 747, in _generate_schema_inner
return self.match_type(obj)
File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 834, in match_type
return self._unknown_type_schema(obj)
File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 393, in _unknown_type_schema
raise PydanticSchemaGenerationError(
pydantic.errors.PydanticSchemaGenerationError: Unable to generate pydantic-core schema for <class 'bson.objectid.ObjectId'>. Set `arbitrary_types_allowed=True` in the model_config to ignore this error or implement `__get_pydantic_core_schema__` on your type to fully support it.
If you got this error by calling handler(<some type>) within `__get_pydantic_core_schema__` then you likely need to call `handler.generate_schema(<some type>)` since we do not call `__get_pydantic_core_schema__` on `<some type>` otherwise to avoid infinite recursion.
For further information visit https://errors.pydantic.dev/2.6/u/schema-for-unknown-type
How can we achieve this?
Thank you.
Hello,
I am trying to store the decimal value in by embedded document:
Class Achived(Document):
score_from: float = mongoz.Decimal
score_to: float = mongoz.Decimal(max_digits=5, decimal_places=3)
amount: float = mongoz.Decimal(
max_digits=5, decimal_places=3)
When I try to store the decimal value it raise the exception as:
InvalidDocument("cannot encode object: Decimal('0.60'), of type: <class 'decimal.Decimal'>")
.
As I passing the string or float value for it to store in document.
Hello,
I have a models as:
class BaseDocument(mongoz.Document):
"""
The base document for all documents using the `registry` registry.
"""
objects: ClassVar[CustomManger] = CustomManger()
class Meta:
"""
It contains the Meta information related the database for \
each model level.
"""
abstract = True
registry = settings.client
database = settings.db
class Employee(BaseDocument):
month_period: int = mongoz.Integer(null=True)
full_name: str = mongoz.String()
employee_code: str = mongoz.String()
date_of_joining: datetime.datetime = mongoz.DateTime(null=True)
e_mail: str = mongoz.String(null=True)
mobile_no: str = mongoz.String(null=True)
When I am trying to query into db, Its using the default manager i.e. "Manager" object, instead of "CustomManager".
If I again initialize the "objects" in the Employee Document and try to query as:
Employee.objects.create(full_name='Emp_Name_1326', employee_code='01326')
Then, it returns the API response as:
{
"detail": [
{
"type": "missing",
"loc": [
"month_period"
],
"msg": "Field required",
"input": {
"full_name": "Emp_Name_1326",
"employee_code": "01326"
},
"url": "https://errors.pydantic.dev/2.6/v/missing"
},
{
"type": "missing",
"loc": [
"date_of_joining"
],
"msg": "Field required",
"input": {
"full_name": "Emp_Name_1326",
"employee_code": "01326"
},
"url": "https://errors.pydantic.dev/2.6/v/missing"
},
{
"type": "missing",
"loc": [
"e_mail"
],
"msg": "Field required",
"input": {
"full_name": "Emp_Name_1326",
"employee_code": "01326"
},
"url": "https://errors.pydantic.dev/2.6/v/missing"
},
{
"type": "missing",
"loc": [
"mobile_no"
],
"msg": "Field required",
"input": {
"full_name": "Emp_Name_1326",
"employee_code": "01326"
},
"url": "https://errors.pydantic.dev/2.6/v/missing"
}
]
}
Can you help me for this?
Is somthing wrong here?
Thank you.
Originally posted by harshalizode May 6, 2024
Hello,
I have the Permission Model as:
class Permission(mongoz.Document):
idx: int = mongoz.Integer()
code: str = mongoz.String()
I am trying to sort the permissions in Ascending order.
await Permission.objects.sort("idx", Order.ASCENDING).values(["idx"])
But I am getting the result as
Can you confirm that this is an issue or a mistake that occurred while running the query?
Thank you.
Hello,
I have tried to rename my collection as per the documentation https://mongoz.tarsild.io/documents/?h=collection#document-without-table-name, but here is an issue.
When I am trying to add the collection in the Meta class then, it is raising the issue as
class Company(BaseDocument):
File "/home/venv/proj.env/lib/python3.11/site-packages/mongoz/core/db/documents/metaclasses.py", line 290, in __new__
collection_name = meta.collection.name
^^^^^^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'name'
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.