Django custom model template - django-models

I want to create a custom datetimefield. I know that models.DateTimeField use the template admin/widgets/split_datetime.html
I've modified that template to accomplish what i want.
But now i want two have the original DateTimeField in same models and a new one in anothers.
I've create my new model.
class DateTimeFieldHorizontal(models.DateTimeField):
template_name = 'mydjango/widgets/my_split_datetime.html'
But this model also use original split_datetime.html.
'mydjango/widgets/my_split_datetime.html' is in template folder
I didn't found how to change models template file.
Thanks.

Related

Where to place a non-model database fetch architecturally?

I am developing a rich client application using Backbone.js and have encountered a situation where I need to fetch 3 values from three database tables and display them.
The rub, however, is that they are not part of my models and I have no need to synchronize them. They are for display purposes only. Should I created a child view which adds them to the DOM as part of the render() method? I'd prefer not to just hack some jQuery code together -but- it doesn't seem to fit nicely into my mental picture of Backbone models and views either.
The rule of thumb in MVCs like Backbone is that the data structure should always be stored in the model layer imo. In your case this could either mean extending your current models with those ui state related attributes (recommended) or create a new model and pass it to the views as an option:
var myModel = new MyModel();
var newModel = new NewModel();
var myView = new View({
model: myModel,
newModel: newModel
});
later in the view you can access to the newModel as this.options.newModel

Data-binding in Widgets

I am trying to data-bind a app/Models/mymodel.js in app/widgets/mywidget/widget.xml
<Collection src="mymodel" instance="true" id="aModel" />
I get the following error:
[ERROR] : Script Error Couldn't find module: alloy/widgets/mywidget/models/mymodel for architecture: x86_64
Not specifying WPATH in widget/ctrl.js and widget/style.tss resulting in Alloy.create* methods pick up from app/ controller or models.
Is there a way to specify to use app/Model in widget/xml
Widgets are independent. They're supposed to be shared across apps. So having a dependency on the app, or a specific model, is not the way it is supposed to be and it is designed so it won't work for this reason.
If you've written the widget yourself specifically for this app remove it, and move code to a separate controller.
If you want to share the widget across apps and want to use a collection inside it, make an exported function and provide the collection to it.
In your widget create a model file with a generic name. Include that collection inside your widget.xml. Then in your widget.js create a method to import the collection
exports.setCollection = function(collection){
$.myCollection.reset(collection.models);
}
Then in your controller including the widget:
$.myWidget.setCollection($.myOtherCollection);
This will set all models of the imported collection to the widget collection. Have an ID attribute that doesn't match? Do some converting in the setCollection method so ID does match. That way it is reusable across apps.
For example, your ID attribute is ObjectId, then you this:
exports.setCollection = function(collection, IdAttribute){
_.each(collection, function(model){
model.set({id: model.get(IdAttribute)}, {silent: true});
});
$.myCollection.reset(collection.models);
}
Then in your controller including the widget:
$.myWidget.setCollection($.myOtherCollection,'ObjectId');
Then you've transformed your collection and all should work

Backbone.js + Handlebars.js - {{#each}} instead of model view?

I have a Collection called Projects that contains several models of type Project. When I want to list all the projects, I use a ListProjectsView. Its render function generates a ProjectView for each project in the collection, making it possible to delete one single project by binding an event to its name.
However, now I want to use Handlebars for creating templates. With handlebars it easier to display all the collection models, with the each helper, like this:
<ul>
{{#each projects}}
<li>{{this.name}}</li>
{{/each}}
</ul>
This works fine but now I am wondering how can I delete one project, since I am not using ProjectView anymore. Do I need to add an id to each <li> so I can bind an event? Or do I have to use ProjectView to do this?
Thanks in advance.
the logic here will have to run through ListProjectsView. One way is to do as you say and attach an data-id to each li and then in your event handler get the target
deleteProjectHandler : function(evt){
var $target = $(evt.target), id = $target.data('id');
this.deleteProject(id);
}
Or something of the like.

Backbone Models - extracting names at runtime

I need to be able extract a backbone model's name at runtime from a backbone View to write generic View and then Template code. That is: I need to get the "ClassName" from the model that is passed to the View and then take the first three characters from it and pass it on to the template.
Is there a simple way to do it?
Thanks.
Bharat
By ClassName, do you mean the name by which you refer to your model, such as User in new User();?
If so, unfortunately this is not possible, because the name you give to the model class is simply a variable name and not part of the model declaration. Consider:
var Foo = Backbone.Model.extend({});
var Bar = Foo;
Is the ClassName Foo or Bar?
Instead, simply give each of your models a name property:
var Foo = Backbone.Model.extend({
name:"Foo"
});
This way you can easily get the model name, either by modelInstance.name or ModelClass.prototype.name.

How Do I Filters the list by refModel('Model_user') in the Models?

I am unable to find how to filter the drown down which i got by using refmodel in my model which extends model_table
thanks in advance
For any view in toolkit you can use setController and getController. refModel fields from Model will set a proper controller for a form field. It uses type reference.
Once form in initialized, you can interact with controller of a field by
$field->getField('myfield')->getController()->addCondition('type','admin');
Alternatively the following might work too:
$field->getField('myfield')->dictionary()->addCondition('type','admin');

Resources