I am working on spree and want to make changes in admin panel as an admin can add only one taxonomy(primary category) and correspondingly add more tertiary categories (taxons) that come from only that taxonomy.
how should i proceed for this?
If I follow, you want to have max one root taxonomy in admin section. When admin tries to create new one, this action should be blocked. If so it can be done on different ways. I would try this:
block creating new taxon if there are already taxon in database - override Spree Taxon model, you might use before_create callback and check if root taxon is in DB and block creation.
in templates you can override admin taxon index and hide add taxon button if there is a taxon already
Related
In django admin, you can add, edit, and even delete objects from another model if there is a relationship between the two.
For instance, if my code looks like this:
class Category(models.Model):
...
class Product(models.Model):
...
category = models.ForeignKey(Category)
When I am editing/adding a product using the django admin site, in the category field, I have 3 buttons to add/edit/delete categories. Adding one takes to a new window, and once I submit the form, the category is added, the window is closed, and I am returned to my product form with the extra category present. Like this:
How can I do this in my normal application (outside the admin) using forms?
If I understand your question correctly, you could do what django admin does, which is to link the add button to this:
/admin/<your_app>/<your_model>/add/?_to_field=id&_popup=1
and then it uses a bit of javascript to get back the new object you just created. If you look into the contrib/admin/static/admin/js/admin/RelatedObjectLookups.js file (in django's code), you'll see a few functions that pass the id of the calling field to the popup (in showRelatedObjectPopup), and then bring back the selected id (in dismissRelatedLookupPopup).
This is for adding a new object, but you can look into the logic for changing/deleting.
You can replicate that logic with your own forms.
I created two models in django. First is about products (name, price etc) and second is shop which contains this products.
How can I edit every product from my shop in one form?
For example i have page with shop detail (all products, prices) and I want on this page edit few prices of products from this shop. I don't want click on every product and edit it, I want do it on one page. Is it possible? What is the best way to do it?
You can use inline forms
# admin.py
class ProductInline(admin.StackedInline):
model = Product
class StoreAdmin(admin.ModelAdmin):
inlines = [ProductInline]
admin.site.register(Store, StoreAdmin)
With this configuration, you will be able to edit / add / delete related Products on the Store admin page.
Solution 1
The Django admin site, acessible at http://<myurl>:<myport>/admin/, will allow you do edit saved model data. Can't remember off the top of my head if you can edit multiples or not though..
Solution 2
Connect to your datasource using a SQL Editor for example, using MySQLWorkbench, and script it, which would allow you to update all the products for one shop in one fell swoop. Some example SQL would be update products set price='4.99' where shop_id=1 or something similar
I have two models: Customer and Resources. Customer has a ManyToManyField to Resources. I add the relations from the admin panel e.g.:
Customer_1 -> Resource_8, Resource_5, Resource_14
The main problem is that I want to insert these relationships in that exact order: resource 8, 5 and 14. But when the admin form is submitted, it saves the relations as follows:
Customer_1 -> Resource_5, Resource_8, Resource_14
I assume that it is ordering the resources by its ID and not respecting the order they were submitted.
My question is, how do I change this behaviour, so that it always respect the order which was submitted on the admin site?
Django doesn't support ordering in M2M fields, you will need to alter the way your model is defined, specifically you will need to create a through table and use a custom ordering field for it.
I'm a little confused as to why this sort of functionality isn't default in the admin, but maybe someone can give me a few hinters to how to go about it.
I have a projects application which keeps track of projects and is to be edited through the admin. Each project has numerous ForeignKey related models (links, flatpages, video, image etc.) that could be placed as inlines within the project admin.
(One or two models have nested inlines, so they don't display in the admin (this and this ticket deal with this) )
Instead of being able to edit these models inline on the project admin (which gets messy and difficult to use), I would love a list of all the current instances of that related model, and simple add/edit button for each model which opens a popup with that model's form.
Project Admin:
- Normal Fields
- Links:
-Link 1 (edit)
-Link 2 (edit)
+ add link <- popup
- Images:
-Image 1 (edit)
-Image 2 (edit)
+ add image <- popup
so on. How would I go about writing this? I only need to do it for one section/model of the admin panel so I don't think writing my own Crud backend is necessary.
Thanks
I implemented something like this in an application once, but since django-admin doesnt support nested inlines (by which i mean inlines within inlines), i followed a slightly different approach. The use case was that you had an invoice (with a few inline attributes) and u had reciepts (again with inline attributes). Reciepts had a foreign key to the invoice model (basically a reciept was part payment of the invoice).
I implemented it by adding a field to the invoice list view which linked to a filtered reciept list view.
So in the invoice admin, there would be:
def admin_view_receipts(self, object):
url = urlresolvers.reverse('admin:invoice_%s_changelist'%'receipt')
params = urllib.urlencode({'invoice__id__exact': object.id})
return 'Receipts' % (url, params)
admin_view_receipts.allow_tags = True
admin_view_receipts.short_description = 'Receipts'
This gives you a link in the list view that takes you to another list view, but filtered by foreignkey. Now you can have inlines for both models and easy access to the related models.
Consider a wiki application. There is a model Page, that has many Revisions and each revision has many blocks.
What is the simplest way to create an admin in which, you select a page and all the blocks of the latest revision appear; bonus points for letting change of revision by a dropdown (which is by default, sorted in reverse order anyway)
Is it absolutely necessary to create views, or can I extend some of those StackedInline forms, override save and mention some magic meta options, to get it all done automagically.
Have you tried something like this (in admin.py):
class RevInline(admin.TabularInline):
model = Revision
class PageAdmin(admin.ModelAdmin):
model = Page
inlines = (RevInline,)
admin.site.register(Page, PageAdmin)