Django: Defining custom fields to work with Angular Material - python

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.

Related

Django pure controller functions

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?

python application link models and views

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.

Django modelform: Create new related object

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.

how to make dynamic field creation capability in openerp module?

Hi I have created an openerp module using Python (eclipse) . I want to add a feature in my form so that admin will be able to create his own fields whenever and whatever he wants . I needed some guidance of how this will be done . As I am new to openerp , any help will be good to me . Thanks
Hopes for advice
I can't think of any easy way of doing this. When OpenERP connects to a database it sets up a registry containing all models and all the fields and as part of this, loads the fields into the database, performs database refactoring etc. The idea is that it is simple to inherit existing models and add your fields that way but it does require coding.
I have done something similar where:
I predefined some fields on your model (field1, intfield1, charfield1 etc.).
Provide a model/form so the admin can say use intfield1 and give it a label of 'My Value'
Override fields_view_get on your model and change the XML to include your field with the correct label.
But this is tricky to get right. You will want to spend some time learning the elementtree module to do the XML manipulation in the fields_view_get.

Django web interface components

I am using python and django and like it a lot. But than i use it, I catch myself thinking
what i do a lot of work to render result data and write specific actions for it. For example than i pass result set of objects to template
i must render all data and write all possible actions such as sorting by columns,filtering,deletion,edit etc, for each of this i need
to write code in urls.py and views.py, sometimes helps generic view but it's has poor functions.
Is there some solutions to automate this work?
i mean use some interface compontents (such as "model list renderer with column filter and pagination") to wich i need only
"bind my model", all other routing work for drawing common interface action must be allready implemented in these components.
i think i need something like configurable components for fast building html web interface for models (such as model forms do fast generation forms for models).
What do you think can help in this case?
must render all data and write all
possible actions such as sorting by
columns,filtering,deletion,edit etc
Like django.contrib.admin? But I guess it's way to complicated and bloated for your needs.
sometimes helps generic view but it's
has poor functions
And that's the way, I think, you should be going. If you write same views over and over again, just make your own generic views. As an example of more robust views and a source of inspiration I recommend you to look at class-based generic views.
Also consider using model inheritance and custom managers.

Categories

Resources