I have the Data in below format
and the Model i have it is as below
Now I want to form this model with the data available in the table with Main parent would be as below recursively in Python
Can someone help me on this.
Related
I am new with Django but I understand that the aim is to never reinvent the wheel. I am developing an application to supervise a factory and in all the html file I need to implement a tree representing the structure of the factory.
-> Factory
-> Production Area
-> Machine 1
-> Machine 2
etc
So I decided to put this tree structure in the base.html file and extend it on every others html files.
The problem is that I need to extract the element from my database, using the views.py files in order to call the render function to send it to the html file.
My question is how to access this data in the base.html, because he dont got any views.py to use a render function.
Thanks, Baptiste.
If you want something to show up in every template of your website (a tree in your case), you can put it in a base template and inherit from that template.
I personally (and many others as well) use a 3-level template structure. You can find an example/explanation in this GitHub repo and this post. Feel free to adapt the idea to your needs.
So in your case you could put your tree structure (the factory structure/layout) in your base template. If the data of the tree has to be fetched from the database, then you could use a custom context processor so that you don't have to repeat the tree-fetching-code in every view.
I have a model that will be displayed in a ListView. One of the fields in the model is category which has 3 choices. When I display the template, I want to be able to tell between the 3 categories. I considered overriding get_context_data() to add a context for each category by getting all the objects and filtering them.
Would that be a better approach than a custom template tag?
And even if it is, assuming I still want to create the custom template tag to accomplish this, how would I write it? I know how to write custom tags, but I am unsure how to write one such as this.
Only idea I can come up with is to create a tag something like:
#register.simple_tag(name="is_cat1", takes_context=True)
def is_cat1(self, context):
objs = context['object_list']
if (MyModel.objects.filter(category__icontains="Cat1") in objs):
objs = MyModel.objects.filter(category__icontains="Cat1")
return objs
Could anyone provide an example of such a filter that deals with models like this? As well as answer my question as to whether it would be better to use context?
Thanks
Using AJAX is the correct solution. Such an operation doesn't fit with Django's framework and intended use.
Hi I am working on an openerp module . I want to make a field dynamically . I want to take a name of a field from user and then create a field to it . How this can be done ? Can I do it with fields.function to return name, char type ? Plz help
Do you mean you want a dynamic field on the form/tree view or in the model?
If it is in the view then you override fields_view_get, call super and then process the returned XML for the form type you want adding in the field or manipulating the XML. ElementTree is your friend here.
If you are talking about having a dynamic database field, I don't think you can and OpenERP creates a registry for each database when that database is first accessed and this process performs database refactoring at that time. The registry contains the singleton model instances you get with self.pool.get...
To achieve this you will need to create some kind of generic field like field1 and then in fields_view_get change the string attribute to give it a dynamic label.
Actually, a plan C occurs to me. You could create a properties type of table, use a functional field to read the value for the current user and override fields_view_get to do the form.
You can create Fields Dynamically by the help of class self.pool.get('ir.model.fields')
Use Create Function.
Hi i wanted to know is there a way through which i could use a tree control in my template
I wanted to add something like
Student
|_Name
|_Address
|_Contact No
I think you guys get the picture...How can i add something like that in my template...Thanks
This has nothing to do with Django. You can use any HTML/CSS/Javascript in your templates, so just use any script to create your tree control.
Here is one of jQuery scripts: http://www.boarsoft.com/javascript/dtree/?jdfwkey=97rdf1
Other examples: http://www.google.lv/search?sourceid=chrome&ie=UTF-8&q=jquery+tree
django-mptt has template tags for displaying tree structures. I'm not sure whether it would be difficult to use them without storing your data as a tree in the database. This could be used to generate markup or json data for some JavaScript tree widgets.
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.