Django: FileField' object has no attribute 'attrs' in class Meta - python-3.x

I have a simple problem. Inside a modelForm, in my Meta class, inside widgets={} i have specified:
'video' : forms.FileField(allow_empty_file=True)
however django complains that 'FileField' object has no attribute 'attrs'. What could be the issue

A FileField [Django-doc] is a form field, not a widget*. The default widget of a FileField is a ClearableFileInput [Django-doc].
You can thus construct a (model)form with:
class MyForm(forms.Form):
video = forms.FileField(allow_empty_file=True)
# …

Related

How to override field name attribute in django model form?

I have a model form similar to the one below:
class BookSearchForm(ModelForm):
class Meta:
model = Book
fields = ['publisher', 'authors', 'category'
How to override fields name attribute in the above model form?
I tried this, but it did not work:
class BookSearchForm(ModelForm):
class Meta:
model = Book
fields = ['publisher', 'authors', 'category'
widgets = {
'publisher': forms.SelectMultiple(attrs={'name': 'pub'}),
'authors': forms.SelectMultiple(attrs={'name': 'aut'}),
'category': forms.SelectMultiple(attrs={'name': 'cat'}),
}
you don't want to change the name of a field in the forms, django needs that to collect data for that field if you don't provide db_column as the name of the db column. what you can do with the first option, if you want the user to see publisher or some other name, in the models, add a verbose_name and then for the actual name you can declare the field however you want. Your code could look like this
pub = models.WhateverField(verbose_name='what i want you to see')
now when you do {{form.pub.label)}}, 'what i want you to see' is displayed in the html. Of course, don't forget to add the actual input in you template, {{form.pub}}. This way, you don't add anything extra in the form to display a user friendly name. I've posted this as an answer as i ran out of characters for a commment.

Is there a way to make LitHtml attributes optional?

I'm not talking about boolean attributes, attributes like class if you don't want to add an empty class attribute if there's no CSS class.
html`<span class=${hasClass ? 'my-class' : ''}></span>`
There is an ifDefined directive that does what you want. If the value is undefined the attribute won't be rendered.
import {ifDefined} from 'lit-html/directives/if-defined';
html`<span class=${ifDefined(hasClass ? 'my-class' : undefined)}></span>`

SimpleAttribute instead of Attribute in Rapidminer script?

I am trying to extract an Attribute from an ExampleSet in a RapidMiner 'Execute script' like this:
ExampleSet exSet = input[0];
Attributes attrs = exSet.getAttributes();
Attribute attr = attrs.getAttribute("h_area");
but then I get an error and it says that attrs is not a Attributes but a SimpleAttributes object.
This works:
Attribute[] attrs2 = exSet.createRegularAttributeArray();
Attribute attr2 = attrs2.getAt(1);
What is the correct way to get an Attribute from an ExampleSet?
From these docs, it looks like the getAttributes() call will return an object implementing the Attributes abstract class, which SimpleAttributes is, so it looks pretty fair at this stage. However, the getAttribute() method doesn't look like it's defined in either object. I can't test this here and now, but have you tried the following:
ExampleSet exSet = input[0];
Attributes attrs = exSet.getAttributes();
Attribute attr = attrs.get("h_area");

WPFExtendedToolkit PropertyGrid Standard Values

I'm trying to display XmlElement's attributes in Xceed PropertyGrid. For that purpose I defined custom wrapper class. It wraps XmlElement, iterates over XmlAttributes and creates custom PropertyDescriptor for each XmlAttribute. All "virtual" properties' type is String. All works fine.
Now I want to have drop-down list of possible attribute values for every attribute that has restricted set of values. In Xceed's PropertyGrid, there is ItemsSourceAttribute for that. But it has to be applied as follows:
ItemsSourceAttribute(typeof(MyCustomItemsSource))
And here is the problem - I can not provide proper argument for MyCustomItemsSource constructor. What can I do about this?
It seems that there is another possibility - to define a TypeConverter, override GetStandardValues, and supply this converter to "virtual" property. But PropertyGrid just ignores this attribute.
How this simple task can be done with Xceed PropertyGrid?
Solved. I implemented custom editor
public class AttributeValuesEditor: Xceed.Wpf.Toolkit.PropertyGrid.Editors.ComboBoxEditor
{
protected override IEnumerable CreateItemsSource(PropertyItem propertyItem)
{
var property = propertyItem.PropertyDescriptor as XmlAttributePropertyDescriptor;
Debug.Assert(property!=null);
return property.GetCompletionValues();
}
}
Here, the context is passed into method in the form of PropertyItem. Now it is possible to differentiate between different attributes and return appropriate items.

How to get YUI object on page

I'm using the YUI2 framework.
I want to be able to get a reference to an object that was created on the page. I don't want to change the existing JS code to store a global handle to it when it is created, if possible.
The page has something like this on it:
<script type="text/javascript">//<![CDATA[
new FirstRate.Reporter("report1").setOptions(
{
inData: "testfunc"
}));
//]]></script>
I don't see any methods in YAHOO.util.Dom specific to finding objects.
How can I get an object reference to FirstRate.Reporter? Is there anything to ennumerate the objects on the page?
Thanks
YUI contains lots of methods for finding objects in the webpage DOM. YAHOO.util.DOM contains methods:
HTMLElement | Array get ( el )
Array getElementsBy ( method , tag , root , apply , o , overrides )
HTMLElement getElementBy ( method , tag , root )
Array getElementsByClassName ( className , tag , root , apply , o , overrides )
and many more. These retrieve objects from the DOM. To find an object in the page with YUI2, you would typically use some combination of the tag type, the class name or the id to query the page using YUI methods to find the object you want. It's easiest to find a specific object in the page if you give it a unique id value and then you can just using YAHOO.util.DOM.get("myObject") to retrieve it (where "myObject" is the id of the HTML element).
If you want to get regular javascript objects, then you have to store references to them yourself in your own javascript variables. YUI doesn't do that for you except when using some UI widgets (which also have DOM elements).
If you want to keep track of the result of this:
new FirstRate.Reporter("report1").setOptions(
{
inData: "testfunc"
})
Then, you have to assign it to a variable or to a property of an object whose scope allows it to last long enough for you to use it.
var theReporter = new FirstRate.Reporter("report1").setOptions(
{
inData: "testfunc"
})
or
myObject.reporter = new FirstRate.Reporter("report1").setOptions(
{
inData: "testfunc"
})
where myObject is some other object you've created and are storing.
JavaScript objects aren't part of DOM. DOM is the Document Object Model, it's about objects in HTML document like divs, forms, inputs etc, the kind of objects Browser displays.
There is no "global enumeration" of objects in JavaScript. There are global variables.
I don't want to change the existing JS code to store a global handle to it
So you don't want to use global variable. But why?
This is the only way to do it, and it's also very simple:
var myGlobalVar = new FirstRate.Reporter("report1").setOptions({inData: "testfunc"}));
Here you'll store reference ("handle" as you say) to your object in global var myGlobalVar, which you can later access in another part of your JavaScript code.

Resources