I have a page where it displays a filtered model instance list and allows users to update some fields of it or add new fields as a form.
I am curious what wpuld be a clever way of doing this, to delete and resave all the input data or make comparison for each data and save edited / new fields& entities.
I would like to mind you that I use postgres for saving these values and I display around 20 entries for this form.
The QuerySet object has the update() method - it's used in ie. Admin Panel for bulk updating multiple selected objects from change lists. Here is the method reference at django's official documentation.
How to use it:
Just create queryset with the models you want to update (assumng that MyModel has field named 'my_field'):
qs = MyModel.objects.all()
qs.update(my_field=value)
That's it - remember that update() method will not send any signals like the save() method - it will just run query directly to database.
As for 'adding fields via form' - I don't know if I got it right? You want to add additional related models or dynamically add fields to the model table on database?
If you want to add related models then use InlineFormset (http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#inline-form) - it's quite easy to handle.
Otherwise you have to add fields to models' _meta as described here: How dynamic add custom field to model.
Related
I have to design a search form and display the results. I am having some difficulties finalizing the models/collections to be used.
I have a few fields which acts as filters. These fields are select boxes where multiple items can be selected. After an item has been selected from a field, I need to call the API which will return a list of results with this filter and also all the remaining filter options for the other fields based on the first filter.
What I am confused is how should I go about this. Should every select box be a model? Because the options for each select box will be changing.
About the results,
I am thinking that results should be a collection. But given that there is only one API endpoint here, I am confused which model should hold the url.
Since your API is actually returning a collection of results, I'd keep the filter parameters in a model, and while submitting search request, create the collection URL dynamically based on the filter model attributes.
You can go the other way, send the request from model itself, then in parse set results from response to the collection and remove it from model, but it seems more hacky than dynamically creating collection URL
I have a subclass of Wagtail Page class that has field of django ManyToManyField type. When I try to create a new instance of my page object, I get a list of objects that the the ManyToManyField points to and I am able select multiple items. However, after creating that page when I try to edit the same page, it seems no data got saved for the ManyToMany field. I know in Django ModelAdmin one have to override the save_related() to save ManyToMany field data. Is there a similar method for the Wagtail Page model?
You should define the field as a ParentalManyToManyField relation, as per the example here: http://docs.wagtail.io/en/v1.13.1/getting_started/tutorial.html#categories
This is a variant of ManyToManyField which is able to keep track of the relation in memory, allowing it to work in situations such as previewing and saving as draft (where it doesn't get saved to the normal database records).
I was able to use the 'after_edit_page' and 'after_create_page' hooks to save the data for the page's ManyToMany fields.
I have a complex search form with about two dozen CharField and ChoiceFields.
My users want to be able to save specific queries (giving each one a name) so they can run them routinely without choosing all of the fields manually.
Should I just pickle the form object and save it in the datbase?
EDIT: I should note that they want to be able to modify the form choices after the fact as well.
Not the form itself, but the value returned from the cleaned_data attribute.
Is it possible to save ModelForm object data with db_alias different than "default"
my_form = MyModelForm(request.POST)
my_form.save(commit=True,using="db_alias")
as well as saving data with model instance?
Thank you.
Short Answer: Unfortunately ,you can't save the form that way. If you form doesn't contain ForeignKey or m2m fields (or you are controlling them yourself, for example using an autocompletefield, etc.), you can handle the object after the form:
_obj = _form.save(commit=False)
_obj.save(using=_db_alias)
Long answer: If you want the modelform to behave like a normal one with ForeignKeys and m2m-fields, something like:
# The form's foreign_keys and m2m-fields get the data from the db_alias database
# and evertyhing is sdisplayed correctly on the template.
_form = myModelForm(request, db_alias=_db_alias)
# The form saves to the correct DB and foreigns & M2ms are matched correctly in this DB
# _form.save()
Although this would be ideal, you just can't use this behaviour. There are many DB hooks that you need to alter in Django code to get this working. What I have done is to create a new modelform class from the base modelform, and get the (partial) functionality described before.
Hope this helps, and also hopping a better solution comes soon.
quite an explanation here, hope someone has the patience to read it through
I'm building an application in Flex 4 that handles an ordering system. I have a small mySql database and I've written a few services in php to handle the database.
Basically the logic goes like this:
I have tables for customers, products, productGroups, orders, and orderContent
I have no problem with the CRUD management of the products, orders and customers, it is the order submission that the customer will fill in that is giving me headaches:
What I want is to display the products in dataGrids, ordered by group, which will be populated with Flex datamanagement via the php-services, and that per se is no problem. But I also want an extra column in the datagrid that the user can fill in with the amount he wishes to order of that product. This column would in theory then bind to the db table "orderContent" via the php services.
The problem is that you would need to create a new order in the database first that the data could bind to (orderContent is linked to an order in the db).
I do not want to create a new order every time a user enters the page to look at the products, rather I would like to create the order when a button is pressed and then take everything from the datagrids on the page and submit it into the database.
My idea has been to create a separate one-column datagrid, line it up next to the datagrid that contains the products and in that datagrid the user would be able to enter the amount of that product he'd like to order.
I've created a valueObject that contains the data I would need for an order:
Code:
package valueObjects
{
public class OrderAmount
{
public var productId:int;
public var productAmount:int;
public var productPrice:Number;
public function orderAmount()
{
}
}
}
My idea was to use a service to get all products from a certain group, populate an ArrayCollection with the data, then transfer each object in that ArrayCollection to an instance of the Value Object above, add the value object to another ArrayCollection that would the be used as a dataProvider for the one-column datagrid (I would only display amount which would be set to zero at first, but use the other data upon transfering it to the db)
I've tried to use the results from the automatically generated serviceResults that retrieve the products for the datagrid and put in a resultHandler that transfers the valueobjects, however this does not seem to work.
Basically my question is this: Am I approaching this thing completely wrong, or is there a way I can get it to work the way I planned?
Would I need to create a completely new service request to get the product id:s, and price to populate the one-column datagrid.
I'll post some code if that would help.
Thank you if you read this far.
Solved it by creating a Value Object class to hold all the info needed for each row in the grid and from the php service that returned all products in a group, I looped through the result and transfered the data needed into my Value Object.
I then added each Value Object into an ArrayCollection and made that the dataProvider for the dataGrid.
No need to use two grids. I forgot how logic things get when you think of datagrid data just as an ArrayCollection and forget the visual presentation of it on screen.
Put in a few itemRenderers and the whole thing is beautiful!