Basically I am writing a simple shopping cart. Each item can have multiple prices. (i.e. shirts where each size is priced differently). I would like to have a single price field in my admin panel, where when the first price is entered, an additional price field pops up. However I am kind of at a loss as to how to do this. What would be the best way to do this?
Sounds like you want two related models - Item and Option. Item would contain the name of the item, and Option would contain the option - eg size - and the price of that option. You would then set up your admin to use an inline form for Option.
You probably want inlines and some javascript.
You might try checking out the Satchmo codebase. It has something similar that may provide some inspiration. http://satchmoproject.com
Related
I have a model with many country fields and each is a choice field wit list of countries.
The issue is that there's a lot of countries! So each choice field adds a lot of size and rendering time to the page to generate a select dropdown in django admin. (E.g. Making 5 of these read only brought down response time from 10s to 4s!)
I want to see if there's any alternatives known for handling choice field inputs. Since we use ISO-2 for countries, I want to avoid operators having to input values directly as they would not know them.
Ideally I was hoping for some sort of search or select pop-up similar to what happens with raw_id_fields. That way a select list is only generated when needed, but so far havent found
You may try django-jet package or one of its forks. It is fully rewritten django-admin template, and it supports typing search for model choice fields. Or try looking for another package in django packages
I'm trying to get a field of model in Django, which will behave like a ChoiceField but:
will support multiselect
would allow to edit the list of choices
For example - I've got a product, which belongs to several cathegoris: nice, for boys, for men and so on. One product could belong to several categories. Categories can vary across the time.
Currently I'm using MultiSelectField from djang-multiselectfield, which - obviously - handles the first issue.
Is there a ready to use app, which also supports edition of choice list? Or what would be the easiest approach to achieve that?
I'm a beginner in Python and Django.
I have installed django-oscar. Then I Configured it and started the server, it works.
Now, I don't understand how to add a product?
At the dashboard there is a button Create new product. But in order to add new product it asks to select product class and I can not find any product class in the given dropdown options.
Provide me a demo example of how to add product in django-oscar.
You need to be logged in as a superuser and go to the store/dashboard URL
DO NOT ADMINISTER THIS FROM THE NORMAL DJANGO ADMIN CONSOLE (even though that answer was accepted?)
Here is an example of what this looks like in the included sandbox app
You need to add a category, product type, and partner and only then can you begin adding real products
Check out this commit - it hasn't been merged to the Oscar's master yet, but should give you an idea on how you can create products programmatically, for instance when importing data.
https://github.com/ArtS/django-oscar/blob/3f9abaf8d5c179c385b90dfa463a35ff9f92f73c/docs/source/recipes/importing_a_catalogue.rst
There is a separate page to administer product types, complete with a "Create new product type" button.
Using django-admin is not a good solution, you may be able to add product types and products through it, but you'll be missing out on any dashboard hooks into the normal process.
A look at the source code shows that whilst you may be able to add a product without a type (the FK is nullable), you may then experience other problems down the line as oscar expects only child products to have a null product_class.
#: None for child products, they inherit their parent's product class
product_class = models.ForeignKey(
'catalogue.ProductClass', null=True, on_delete=models.PROTECT,
verbose_name=_('Product Type'), related_name="products",
help_text=_("Choose what type of product this is"))
Definitely best to try to work with the system rather than around.
You have to add atleast one product class /admin/catalogue/productclass/
By default, Django's admin renders ForeignKey fields in admin as a select field, listing every record in the foreign table as an option. In one admin-accessible model, I'm referencing the User model as a ForeignKey, and since I have thousands of users Django is populating the select with thousands of options. This is causing the admin page to load incredibly slowly, and the select is not very useful since it can take a while to scroll through thousands of options to find the one you want.
What's the best way to change the rendering of this field in order to improve page load and usability? I'd like the select field to be replaced with some sort of button to launch a search form popup, or a text field that searches keywords via Ajax to find the Id for the specific User they want to associate. Does admin have anything like this builtin, or would I have to write this from scratch?
Add raw_id_fields to your model to only show the ID instead of a dropdown.
You're right, Cerin, the cause of the slowdown is because Django is populating the <select> element with too many options. You might want to use an autocomplete element instead.
Interestingly, Django 2.0 has introduced a new feature on the admin site, called autocomplete_fields, which I think you will find useful in this case. It uses AJAX.
class ExampleAdmin(models.ModelAdmin):
autocomplete_fields = ['example_field_user']
You can use one of the few autocomplete apps for Django. Check them at Django Packages.
There's also django-extensions that have ForeignKeyAutocompleteAdmin that fit your needs pretty well.
Another option is to add readonly_fields instead of raw_id_fields
Can anyone share some pointers on building a Donations module for Satchmo? I'm comfortable customizing Satchmo's product models etc but unable to find anything related to Donations
I realize it's possible to create a Donations virtual product but as far as I can tell this still requires setting the amount beforehand ($5, $10 etc). I want users to be able to donate arbitrary amounts
It looks like the satchmo_cart_details_query signal is the way to go about doing this. It allows you to add a price change value (in my case, donation amount) to a cart item
I'll post the full solution if anyone is interested