I want to save model with single id with more then one item and quantity.
class Cart(models.Model):
item = models.ForeignKey(Item, verbose_name="item")
quantity = models.PositiveIntegerField(default=1)
i guess you can give a ID for every instances that are created on a cart and you can used that ID.
Related
I have a form variable of type external API called customersList. It is a list of customer objects. In my form, I have a Customer Name and Customer Id. For the Customer Name, I use an auto-complete widget. So, when I type something in the Customer Name field, it will give me a list of suggestions. When I click on one of the suggestion, I want the Customer Id field automatically populated with the information corresponding to the name I choose.
For example, the first object of a customersList variable which is:
customersList[0] = { "customerName" = "One Time Customer", "customerNumber" = "0000" }
Thus, when I choose Customer Name field to be One Time Customer, I want the Customer Id to be set to 0000 automatically.
I set the value of the Customer Id field to be customersList[0].customerNumber. But, it is static.
(please note: customerNumber represents Customer Id)
Any tips on how to do it?
Sadly the default autocomplete widget won't let you achieve such use case.
In fact the default autocomplete widget can take a list of JSON object as "Available values", can use one of the attribute of those objects as the display value ("Displayed key" property) and use it for autocompletion but it will only store this value (i.e. the one being displayed).
In your use case it means that you can use your customersList for "Available values" and customerName for "Displayed key" but you will only be able to save the customerName in "Value".
The good news is that a custom widget that do just what you want is available as a community contribution. Checkout the project page to download it and then import it in your UI Designer. An example of usage of this custom widget is also available for download from the project releases.
How can I save a shopping cart order and order history? The items price is different according to add-ons attached with it. Can I save as an object ? or use different rows for each item and link or any better and efficient ideas. addons are kept in different class and items in different class.
Maybe,one of the possible implementations of the shopping kart is to have one class Kart where you save the different attributes of the shopping kart. If there are certain properties of it that won't change over time then one option is to create a class to save the shopping kart identifier alongside the attributes that don't change and then in a separate class have the pointer to the original class that represents the shopping kart (by having a pointer to the row in Kart) and rest of the properties that do change (for example the total, or the items).
In this separated class SepKart, it is a good Idea to have the attributes validSince and validUntil, and to have in the beforeSave trigger, a function that sets the validUntil attribute of another row in SepKart that points to the same shopping kart in Kart and doesn't have it's validUntil set.
In the case of the items and the order of them, a list of objects that point to the items would do it, because then, if the order changes, then just saving a new instance of it would save the new order and you would then be able to see what is the history of the shipping kart in the time A just by querying in SepKart for the row that points to the shopping kart Kart, has a validSince of a time before A and a validUntil after A (or validUntil not set).
I have Added 2 text box in product page to recive height an width from user for each product.
I have used textbox input just like quantity in The buy-block Form.
Now I want to save this textboxs in Database and use them in cart page and order page for each cart and product.
I added 2 variable in cartcontroller.php like $qty in function preProcess() .
Now how can I save them in DB?
If I understand, you want your customers to submit custom values along the product they order.
In this case, you should look for "product customization" fetaure of PrestaShop :
http://doc.prestashop.com/display/PS15/Adding+Products+and+Product+Categories#AddingProductsandProductCategories-ManagingCustomization
You can add textbox on product page, customers will type values, these values wiil be displayed on cart and order detail.
High guys,I have three models that i am using in Cakephp,I create 3 views for my models which are linked by the second model.The problem is how can i retrieve the id of the first model when i want to do a final save?
Under the assumption that you're saving the three models in three separate page requests, it should suffice to simply store the saved model data in your session. That way you can read it in the 2nd and 3rd page request and include the necessary data in your forms and processing.
If you're doing it all in the same request, then Moyed's idea works, you can get the ID of a saved row through the model:
$this->Model->save($data);
$saved_row_id = $this->Model->id;
You need to know the id of first model.
If you use the the save() method you can get the id like so:
$this->Model->save($data);
$id = $this->Model->id;
OR
Cake's model class has a function that gets the last inserted id:
$this->Model->getLastInsertID()
You just need to replace Model with the name of first model
I've got two models: Group and Item. An Item has a list of tags and belongs to a Group.
class Group(db.Model):
name = db.StringProperty()
class Item(db.Model):
title = db.StringProperty()
tags = db.StringListProperty()
group = db.ReferenceProperty(Group)
So far, typical actions are adding a tag to an Item, removing a tag from an Item, and showing all Items matching a given Group and tag.
What's a good way for getting a list of all unique tags used within a Group?
Ideally I'd like to have a property in Group that reflects the tags used:
class Group(db.Model):
name = db.StringProperty()
aggregated_tags = db.StringListProperty()
It would be even better if this included the number of Items that have this tag.
Permanent consistency is not a requirement, i.e. it is fine if the aggregated list of tags does not match the actual list of tags in use, as long as they become consistent eventually.
Item and Group are not in the same entity group, so I can't have a transaction that updates the Item and the Group at the same time.
The best way to do this is to maintain it yourself. Update the list of tags whenever you add or remove one from an item. You can do this in a task queue task if you want to do it asynchronously.
Alternatively, you could write a mapreduce that you run periodically that recalculates the tag set for every group - in fact, this is pretty much a classic use-case for mapreduce.