-
In contrast to many tutorials I have the my models in a separate file (see simplified example below). But how can I instantiate the Admin() package in main.py and import it in my models file to add the model to the adminpanel? -> this leads to a circular import.
In flask I could do Any help is highly appreciated! Thanks a lot for your amazing sqladmin (I was looking for something like this for a long time!) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Hey @simonsax I think one workaround is that you do |
Beta Was this translation helpful? Give feedback.
-
@simonsax, along the lines of what @aminalaee is saying here a couple of examples to avoid the circular import: I have an authentication backend as well that requires I have the models being loaded before initializing the admin.
then models.py would look like this:
Personally, I like to separate my model views from my models by putting them in an admin.py, along with any other admin related classes & things, and then use a create_view function in admin.py to dynamically create my views by passing the model from main.py (my views share the same name as the model, just with 'Admin' added btw otherwise you'll have to pass the name in create_view as well):
then main.py on_startup would look like this:
To avoid circular imports, I like to use an init function for most modules & classes and import them after any dependencies have been initialized:
Hope this helps. -- les |
Beta Was this translation helpful? Give feedback.
@simonsax, along the lines of what @aminalaee is saying here a couple of examples to avoid the circular import:
I have an authentication backend as well that requires I have the models being loaded before initializing the admin.