Python/Django application with dynamic model name (application reuse) - python

excuse me in advance if this is not the right title for the problem but here it is:
You have application that works with pre defined model. What happens if you want to
use this application one more time in your project but pointing to different model (same structure but differen name).
For example - you have a "News" application that is fully working but you want to have also and articles the do the same job but you want it in different table.
I'm pretty sure that copying the whole application and renaming the Model isn`t the "pythonic" way so if someone knows how this is done please share your knowledge.
thanks in advance,
Ilian Iliev

This is what abstract models are for. Define once, and all children will acquire the fields in the abstract model, plus be able to define additional fields.

Related

contenttypes framework in Django

I am reading Django documentation (https://docs.djangoproject.com/en/3.0/ref/contrib/contenttypes/#module-django.contrib.contenttypes).
I did not understand content type app, the Django docs describe it as below
Django includes a contenttypes application that can track all of the models installed in your Django-powered project, providing a high-level, generic interface for working with your models.
Can someone explain this from beginner perspective ?
I have experience in developing websites in Django but never touched in this app.
Example: you want to make user-related logging, something like an audit module in user profile. You have dozen of models that could be logged. User, Order, Task, Product, etc.
So your main model will be something like AuditEntity.
How to make it general for all possible current (and future models) that could be logged here? Which type of field do you need?
The answer is Django contenttypes.
If you want - try to implement such a model's structure to understand how it works.
I think that what is trying to tell you is that you can have different content (in blocks, as it is organized in "blocks" and it is very "module friendly", I hope to give the idea), this content is made up of different things, especially models, let's just say that they are like big classes of things needed in your application (don't judge this expression, I know it is not professional but is the more beginner friendly I can write now), hope this makes more clear!

Modular python admin pages

I'm building a personal website that I need to apply modularity to it for purpose of learning. What it means is that there is a model that contains x number of classes with variations, as an example a button is a module that you can modify as much depending on provided attributes. I also have a pages model that need to select any of created modules and render it. I can't find any documentation of how to access multiple classes from one field to reference to.
Model structure is as below:
Modules, contains module A and module B
Pages should be able to select any of module A and order its structure.
Please let me know if not clear, this is the simplest form I could describe. Am I confusing this with meta classes? How one to achieve what I'm trying to achieve?
I ended up using Proxy models but will also try polymorphic approach. This is exactly what is designed to do, inherit models from a parent model in both one to many and many to many relationships.

Best way to use a PyQt5 Model within a PyQt5 TreeModel

I have a code structure already using SQLAlchemy to make declarative models.
Example Tables/Models:
Repository
repository_id
name
owner
Endpoint
repository_id
endpoint_id
name
method
Contexts
context_id
endpoint_id
parent_context_id
config
created_on
created_by
I'd like to make a QtTree to select a Repository > Endpoint > Context > Nested Context.
Similar to how an IDE may have a browser view that shows both folders/packages/files and give different right-click menus to each.
I'm trying to understand what the "proper" way of implementing this would be.
I've looked at GitHub to see what other people have done, but most of small open source apps just use a TreeWidget and have more or less hard-coded views or large projects like "Eric" which are doing things like Lazy Loading... and I'm not sure if it's total overkill for what I'm looking to do -- the 1700+ lines of custom implementation code in Eric has seemed to give more feedback than anything else I've seen... but I still have the issue that my model issue is already semi-defined once...
Since I'm primarily using SQLAlchemy for the rest of the application, should I first create/use a "SqlAlchemyTableModel" to scaffold each model, then create a "BrowserModel" that performs the logic to create these?
Should the RepositoryModel/EndpointModel/ContextModel be a subclass, or is it sufficient to just create them through a generic class?
Ideally I'd like the Repository/Endpoint/Etc to have different icons in the list and different ContextMenus. Should these be set in their model? Through the parent model? Through a decorator proxy? Through view delegates?
I feel like I have a million hammers but I have no clue if what I'm looking at is a nail or a screw.
Reference:
https://github.com/davy39/eric/blob/master/UI/BrowserModel.py

Highly customized admin - Create an app or hack the source code?

I have been facing a big challenge with django since I was assigned the task to rebuild the admin page of our platform. I started unsure of what to do and now that I am half way on, I am even more unsure.
Here is my question:
If I want to build a highly customized admin, should I do a giant hack on the source code or create a new app?
1) Giant Hack
That is how I started and got stuck. A couple of problems appeared on the way, for instance:
I had to extend the AdminSite and override every view I wanted to change
Django's admin is very modular (I mean, very very modular) and a screen might be the sum of tons of other templates
Creating a simple navbar is a pain in the ass (at least it has been for a newbie like me). This is due to the fact that app_list (the variable that contains the mapped models to show in admin's first page) not being accessible when other templates rather than base.html are injected. Thus, I would have to create a context processor, replace every TemplateResponse() by render() (technically, since I tried to do it and it didn't work as well)
The problems go on and on. Given my lack of experience with django, I might be doing crap, but it does feel like I am on the wrong way.
this guy's answer motivated me to post this.
2) Building a brand new app
Of course it will take a little while to implement this, but it seems like a robust and maintainable way of getting it done. One of the points is that I will need to give the same flexibility as the default admin site gives when adding models and promptly having their cruds and tables (I am not really sure how to accomplish this behavior).
I am pretty new to django and any guidelines will be highly appreciated. Give me your thoughts on how to make it.
There is no need to overwrite admin views. The easiest way is to overwrite the templates and add your own css & javascripts.
I have added my own navbar, sidebars and bottom. It is really easy if you overwrite the admin base templates.
If needed you can provide custom data to the template by using own templatetags.

Extending existing django applications

I am using an existing django app called askbot and which to extend some of it's features. Specifically I need to add new relationships into some of it's models.
This poses a problem for me as I would have to make alterations to it's model code, which I'd rather not do as this will break updatability etc.
Is there a better way to do this other than hacking other peoples code?
I've looked at options and considering either creating a app that applies patches to askbot's code or extending it's model, de-registering it and registering my own subclassed model, but I'm unsure of the best way to go about this.

Categories