I am trying to create a python gui application where I need an MVC like pattern to display and control models.
My issue is that I will create and modify the models over time and I need to create several different "view types" (like a form view on one window and a map view on an other), each "view type" should be able to show each of my models.
If I use an MVC pattern (which I am not even sure is relevant), I should then create a view-controller for each of my model and "view type".
So if I create a new model, I will have to create a view-controller for each of the existing "view types", and if I want to create a new "view type" I will have to create a new view-controller for each model.
Creating a generic view is hard because the models are quite independant and differents.
Is there a good pattern or example I could use so I can make this smarter ? I'm stuck with this model / view design...
Thanks for ideas.
I believe you can this by using MVC. You don't have to create a lot of controllers for each model, you can just design GET-parameter like ?view= or specify endpoint to read view type and after this to handle it in a single controllers.
Related
I know how to make it possible to create/edit/delete a multiple-row database model through django admin page. But let's say I want to make it possible to modify the single banner image of my site through admin page, what am I supposed to do then?
Should I create an entire database model for one item? Or is there a better way to implement this?
You can either create your own model for these types of settings, or you can use one of the many packages designed to handle this type of situation:
https://djangopackages.org/grids/g/live-setting/
I want to dynamically create multiple dropbox fields as I input the number of fields to be created.
For instance, there are 2 fields:
integer input field
dropdown menu
so when I input(let's say "4" in field 1) then 4 dropdowns should be created.
How should it be done? And upon submitting the form the same will be stored as a list in a database (flask is used at the backend).
Please guide.
Thanks in advance.
Flask is rather lightweight framework so to create fields dynamically use Javascript. Note nowadays its preferred to develop websites using some advanced JS library or 'framework' JQuery, vue, react etc. On backend you create a controller to save the data. Start with going though a tutorial to understand the procedure. https://www.youtube.com/watch?v=6N4OmsfwfdU . If you use WTF find a tutorial or discussion specific to WTF.
I am wondering on how to implement pure controller functions in a Django's' "biased" MVC scheme. Let me explain it on an example.
Let's say I have a model of an Invoice, which has some attributes (say net, gross etc.). I can present it to the user using a view + template. And that's fine and easy.
But now, I want to send this invoice to a client. This is a more complicated thing, inluding more models (i.e. create an addressed Package model, get a number and let's say few other thing including creating and modifying not only Invoice model itself, but also creating and updating few other model types and instances.
I want this "action" to be available in multiple places of my web application, so going by the book I need to create a view with those actions implemented and bind it to some URL. Probably it should be implemented in POST action.
My questions are:
What kind of generic view should it be (just View? DetailView? other?).
Where should this View redirect after succesfull "send"? The simplest answer would be to redirect to the same referring page, but is this a correct way?
What if I want this "action" to be ran in background (say, send all unsend invoices at midnight) using celery or such? Of course I can make this a celery task and call it in a view. But is this clean django'ish solution? Where do you store such pure business methods in an app/project?
I'm working on an existing Django project to upgrade it's GUI to Angular Material. I've used Django forms and model forms throughout the project and now it's very difficult to write the Angular Material like custom tags and widgets in the templates to replace each form. So, I was trying to write some code which can covert all my default Django form fields to Angular Material tags.
I found two ways of doing so:
Writing a new render function say as_md() with a custom def _html_output(), similar to as_ul() or as_p() by
inheriting Form/ModelForm class. This will convert the complete form fields to the new material tags, however this is a less flexible way (in case I want to change the layout for some of the forms).
Writing custom widgets to render each field as per my requirement.
This will require writing custom widget for each field type.
Can there be some other way to implement this in a more efficient way? Any pointers will be highly appreciated.
Custom Fields
If you need the two way data binding, your fields will each need an ng-model to keep the binding intact. Writing custom widgets will help you achieve this.
But to make sure all your forms will use these custom widgets, a custom render function will work great.
django-angular
If you're looking for something with less hassle, take a look at the django-angular project, it aims to achieve the same thing.
I've been searching stack overflow and google for a solution for over an hour now, and I can't seem to find something that, in my opinion, should be easy to obtain (as it's a common use case).
I've checked this thread, and a few others, but I haven't been able to find a real, easy solution:
Django modelform: is inline adding related model possible?
Anyway, say I have a model with three related entities, two foreign keys and a many-to-many related class. Now, I have a ModelForm which displays these in comboboxes and lists, but what I need is that "+" button next to these elements (as seen in the admin interface).
I want the plus to take me to a new form, for that particular entity, allow me to submit the new information, create the database entry, take me back to my original form and have the newly added entity selected in the combobox. I'm really hoping the django ModelForm Meta class has an attribute that I can't seem to find which enables exactly this.
This isn't really a django question.
This has to do with presentation of a particular widget in an html document, and that is governed by either the HTML markup, CSS, or javascript.
Django is a server side application and is primarily responsible for creating a valid http response and receiving a valid http request (of course, there is a lot that happens in the interim and that is why django is so big) but it's not a "one toolkit to kill them all" app.
I think you want to look at bootstrap: http://getbootstrap.com/
Jquery UI: http://jqueryui.com/
Or some combination of the two.
You can also just mark up the document yourself with a stock img or something.
However, if you want to do it exactly how the admin does it, just go into django.contrib.admin and examin the code to figure out how the django developers did it. I believe they are just using Jquery UI and some manual markup to accomplish that.