Custom ordering of fields inside Formalchemy Fieldset - python

We have a pretty long table where a row should be rendered using FormAlchemy.
The requirement is that the 'title' column should be displayed first and all other fields should follow in alphabetical order. Is there a straight forward way to move and sort fields in FormAlchemy. I need a generic solution here....is touching the FieldSet._render_fields OrderedDict appropriate?

Passing the include paramter to the configure function of the fieldset will allow you to set the order. Are you looking to set it programatically? If not you can do
fieldset.configure(include=[fieldset.title, fieldset.a_field, fieldset.b_field, fieldset.c_field])

Related

How to annotate a Django QuerySet with a Point?

Lets say I have a model A that has fields lat(FloatField) and lon(FloatField). I want to annotate the QuerySet using a point:
A.objects.annotate(point = Value(Point(F('lat'), F('lon')), output_field=PointField()))
I keep on getting TypeError('Invalid parameters given for Point initialization.')
For some reason Django is not recognizing the fields and instead is passing them as strings (I think).
How do I accomplish this?
Thanks
I personally believe you're better off writing a one time migration to add the point field rather than annotating this, but you are just passing strings ATM. You need to use F
A.objects.annotate(
point=ExpressionWrapper(
Point(F('lat'), F('lon')), output_field=PointField()))
ref: https://docs.djangoproject.com/en/3.0/ref/models/expressions/#using-f-with-annotations

How to sort View items

Setting an setSortingEnabled() to True makes it possible to click the column header name to sort the view's items:
tableView=QTableView()
tableView.setSortingEnabled(True)
But even while the attribute has been set the view will display the items unsorted.
In order to sort the items the header must be clicked.
Question: How to make view go ahead and to sort its items before the header is clicked.
So the view is sorted straight from the start.
You can use QHeaderView.setSortIndicator(logicalIndex, order)
For your example, this would mean calling tableView.horizontalHeader().setSortIndicator(0, Qt.AscendingOrder) to sort the first column in ascending order.
Note that you are passing in logicalIndex which may not correspond to the visualIndex if the columns have been reordered. QHeaderView provides methods for translating between the two if you need it (but I think it is unlikely you will need it).
To sort QTableView() without clicking on its header (assuming the tableView.setSortingEnabled(True) was set) use:
tableView.sortByColumn(0, Qt.AscendingOrder)

why django multivaluedict get returns last element

I have used Django MultiValueDict many times and all the times either I use the entire list stored with a key or I want to use the first value from the list. A common use case is use it as form initial data.
My problem is that by default Django MultiValueDict's get method returns last element.
Do I have to override getitem of MultiValueDict or is there any better alternative ?
You can use:
mv_dict.getlist()[index]
Where index is the index of the element you want in the list. For example 0 to get the first element.
Check https://github.com/django/django/blob/master/django/utils/datastructures.py#L285
But certainly if for some reason you always want to return the first element of the list, subclassing MultiValueDict sounds reasonable. It depends on your use case.

Search not exists in Django

I'm really new to django programming, and I'm facing a problem I don't really know how to solve:
I want to get a list of users who have many string attributes, but only the users whom none of it's attributes is equal to a given one.
I have this piece of code
all_users = list(UserProfile.objects.attribute.filter(type=given).exists())
but this code will return me the users who have that attribute, so here's the question: How I can modify this line (or what lines do I need to add) in order to get the list of users without this attribute
Ps: Maybe I didn't explained myself clearly as I don't really know how to specify my problem in english, but, if you don't know what I'm asking I can try again
Thanks all
You can use exclude:
all_users = list(UserProfile.objects.attribute.exclude(type=given).exists())
To quote the docs:
To create such a subset, you refine the initial QuerySet, adding filter conditions. The two most common ways to refine a QuerySet are:
filter(**kwargs)
Returns a new QuerySet containing objects that match the given lookup parameters.
exclude(**kwargs)
Returns a new QuerySet containing objects that do not match the given lookup parameters.

Fastest way to handle a submit button with a variable in it?

I've got a form with a bunch of items. Beside each item is a "Remove" button. When one of buttons is pressed, I need to know which one so that I know which item to remove.
The way I have it now, each button is named remove-# where # is the index of the item. In order to grab the # though I have to loop over every POST value, check if the key starts with remove- and if it does, parse out the integer.
Is there any method that would be faster than O(n) where n is the number of post vars?
Not that n is so large that this would become an expensive operation, but mostly because I'm curious.
Also, before I get any answers that suggest I use JavaScript and put an onclick event on each button to move the index into a separate hidden input.... I'm trying to do this without JavaScript.
An option would be to put each remove button in a separate form, with a hidden value with a constant name and the value indicating which item to remove.
If the POST values are stored in a dict you could turn this around and instead of looking for something that looks like a remove-# you generate all remove-# and try to use them to index the dict. That would limit the search to O(m) where m is the number of elements that can be removed.
This really is the domain of Javascript. However adding onclick isn't a great way to do this. Use HTML5 data attributes and unobtrusive listeners.. For example, in JQuery/HTML5
<input type='button' class='remove-button' name='remove' data-remove-attr='5'>
$('.remove-button').live("click", function() {
alert($(this).attr("data-remove-attr")); // Alerts 5.
});
Despite the data- being an HTML5 thing, it is backwards compatible with old IE, etc as they always allowed arbitrary dom attributes.
Edit: Note this is O(1) if as your action you did $('#remove-'+id).hide();

Categories