π Beanie ODM Support for starlette-admin β Work in Progress #652
Replies: 6 comments 14 replies
-
Hello! π Was working on this a bit for my own project on my fork here. I didn't see the work you did on your branch under "initial commit" until I was pretty deep into my logic π¦ I was able to get the models to show up, sort, and filter as you would expect. Nested pydantic model fields should work using the converter for "BaseModel". Tested the sorting and filtering on string fields for now. Links and list of links are working on my branch as you would expect. Didn't work on BackLinks yet, as this is more complex (no id to reference in the BackLink itself). I build the MongoDB filter queries using the logic from the mongoengine interface, adapted to work with pymongo. I'm sure some complex models will break the logic. To get my fork working with your FastAPI app: Install pip install "git+https://github.com/alexdlukens/starlette-admin.git@beanie"
# or
poetry add "git+https://github.com/alexdlukens/starlette-admin.git@beanie" In your code from starlette_admin.contrib.beanie import Admin, ModelView
admin = Admin()
... call init_beanie() ...
# for each document model you pass to init_beanie
for model in models:
admin.add_view(ModelView(model, name=model.__name__))
admin.mount_to(app) Will try to merge the codebases if I get a chance. Looks like you have more work on the field sorting that I should add to my code. Also haven't added all of the pydantic types one may encounter into the converter, so will fail for some complex models At any rate, hope this helps for your case. Please give it a try and let me know if you have success edit: got some initial tests, copied from mongoengine to work. But having issues getting mypy to pass, because beanie requires Pydantic v2.x. mypy fails when checking on pydantic v1.10.x Best, |
Beta Was this translation helpful? Give feedback.
-
A new commit is now up. It adds the filters option and also allows using the search bar. Next thing I will be working on is OrderBy for the list page. |
Beta Was this translation helpful? Give feedback.
-
@arnabJ, thank you for starting this initiative. I just approved @alexdlukens's pull request #656 to officially support this. You can also take a quick look before it gets merged. |
Beta Was this translation helpful? Give feedback.
-
@jowilf I am not sure if I should create an issue or not for this, but to keep everything related to beanie at one place: This is my Beanie Model: class PostStatus(str, Enum):
draft = "Draft"
published = "Published"
class Post(BaseDocument):
title: str = Field(..., description="Title of the post")
slug: Indexed(str, unique=True) = Field(..., description="Unique slug for the post")
excerpt: Optional[str] = Field(None, description="Short summary of the post")
content: Union[str, dict] = Field(..., description="Block-based format content, HTML or JSON")
thumbnail: str = Field(..., description="Thumbnail URL")
status: PostStatus = Field(default=PostStatus.draft, description="Post status")
@before_event(Insert)
async def insert_slug(self):
slug = self.title.lower().replace(" ", "_")
existing_count = await Post.find_many({"slug": slug}).count()
if existing_count > 0:
slug = f"{slug}_{existing_count + 1}"
self.slug = slug My CustomView: class PostAdmin(ModelView):
model = Post
fields = [
"id", "title", "slug", "excerpt", TextAreaField("content"), ImageField("thumbnail"), "status"
]
exclude_fields_from_create = ["slug"]
exclude_fields_from_edit = ["slug"]
async def create(self, request: Request, data: dict) -> T:
data["slug"] = ""
return await super().create(request, data) But, the TextArea and Image fields are not showing up. They render as text input. ![]() Am I missing something? |
Beta Was this translation helpful? Give feedback.
-
@jowilf can you shed some light into this line: The _arrange_data method returns a stripped down dict with only those fields which are not excluded. Is there any particular reason we would wanna do this? Scenario: async def create(self, request: Request, data: dict) -> Any:
user = await get_admin(request)
data["created_by"] = user.id
data["updated_by"] = user.id
return await super().create(request, data) Since the created_by field is excluded from showing in the Create page, the _arrange_data method removes my manually added value from the dict. Input: input_data = {'name': 'Animation', 'created_by': ObjectId('68400151399d4c0d9175d64f'), 'updated_by': ObjectId('68400151399d4c0d9175d64f')}
outpout_data = self._arrange_data(request, inout_data). # {'name': 'Animation'} It removed the 2 fields which were excluded and this raises execption: 2 validation errors for Tag
created_by
Field required [type=missing, input_value={'name': 'Animation'}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.11/v/missing
updated_by
Field required [type=missing, input_value={'name': 'Animation'}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.11/v/missing
INFO: 127.0.0.1:62314 - "POST /admin/tag/create HTTP/1.1" 422 Unprocessable Entity I am referencing your ODMantic views.py to create my views.py for Beanie. |
Beta Was this translation helpful? Give feedback.
-
Beanie Backend Plugin for Starlette AdminI've just released a Beanie backend plugin for Starlette Admin: The goal of this plugin is to provide seamless integration with Beanie ODM, while keeping the implementation as close as possible to the existing Odmantic backend. This approach aims to ensure stability and a familiar experience for users already working with other supported ODMs. As this is my first attempt at building a backend plugin, Iβve kept the design simple and minimal to reduce the likelihood of introducing errors. However, given my limited experience, I'm not entirely certain whether some of the issues I'm encountering are due to limitations in my plugin or are expected behaviors based on Starlette Admin's current capabilities. If @jowilf or others familiar with the internals could provide feedback or insights, it would be greatly appreciated. Known Issues
I would be grateful for any testing, feedback, or suggestions from the community to help improve and stabilize this plugin. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hey everyone! π
I'm currently working on a Python package that enables Beanie ODM as a backend for starlette-admin. While starlette-admin already supports several ORMs/ODMs out of the box (like SQLAlchemy, Odmantic, etc.), Beanie isn't officially supported yet.
Following the official guide on creating a custom backend and using Odmanticβs implementation as a reference, I've started building a reusable package that bridges the gap between starlette-admin and Beanie.
π§ Current Status: Work-in-progress
π± Branch: dev
π¬ Feedback/PRs Welcome On: dev-colab
π Repository
π GitHub Repo
π§ͺ Goals
If anyoneβs already attempted this, or has insights, I'd love your feedback! PRs and ideas are welcome on the
dev-colab
branch.Thanks to the maintainers for the extensibility guide β it made this much easier to get started. π
Beta Was this translation helpful? Give feedback.
All reactions