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
Related
I am working on a project, I have a requirement from my client he wants to implement a dynamic paginator in django admin panel. Requirement is when user input 10, ten record will display per page same for 20,30
is there any way to do it.
See ModelAdmin.list_per_page. The default is 100 but you can change it to whatever you like.
class UserAdmin(admin.ModelAdmin):
model = User
list_per_page = 5 # No of records per page
admin.site.register(UserAdmin)
If you want to dynamically change it I'd assume you'd use an ajax with a GET for the page number otherwise you'd have to do a lot more and alter the default admin template used by django.
Check out this approach if it helps you .
i'm using Wagtail CMS to create product catalogue. I created basic page type for product:
class Product(Page):
It has basic fields like title, description, image aso. But i need "something special":
There is special part available in many variants and each product can have some of them. So I created another model, very simple by:
#register_snippet
class Variant(models.Model):
to store all variants. Variant has name and image. There are about 200 products and 30 variants.
My problem is and I don't know how to manage in Wagtail two tasks:
to link Product with Variants (foreign key) with many-to-many relation to select product related variants in same page as other page entities
each relations has additional parameters (2 params) which are relation specific (material and diameter) and again I haven't found how to display and manage such relations in page editor
I know that Django can handle it by inline formsets (django admin supports it out of box), but is there Wagtail-way to get this done and editable by Wagtail editor? I prefer to manage whole product in the same place, not relations separated in django-admin.
Thanks for any help or advice.
InlinePanel is the Wagtail equivalent of Django admin's inline formsets. An example of this is given in Wagtail's tutorial: https://docs.wagtail.io/en/stable/getting_started/tutorial.html#images
In this case, it's setting up a many-to-many relation between pages and images, with an additional parameter (caption) on the relation; your Product -> Variant relation could be set up in the same way.
Part of the model of an app I'm trying to build with django is based on a three-tier structure, like this :
class A(models.Model):
name = models.TextField()
class B(models.Model):
related_A = models.ForeignKey(A)
info = models.TextField()
class C(models.Model):
related_B = models.ForeignKey(B)
other_infos = models.TextField()
So A is composed of some Bs, which are composed of some Cs. Since Django admin doesn't accept nested inline forms yet, I was thinking the ideal way of doing it would be with this sort of workflow : there would be a "A admin page", which would get me a link to click called "Add some Bs to this A", and offer me a page with a B form using inline C forms.
Is there a way to do this simply from the Admin website that I didn't see, or should I consider extending the admin default application ?
Some clarification
Let's say you make statistics about Continent, Countries and Regions. My goal is to have a clear structure to add a Country to a Continent and a Region to a Country. Ideally, a user of the administration interface won't be able to add "immediately" a Country ; she or he needs to go through a page where he'll have to select a Continent. Ideally, there would be one single page for the Continent, containing an inline form of country, each of those forms having an inline form of regions.
Since nested inline forms do not seem to be a feature in Django 1.3, I'm looking for a way to have a sort of "workflow" in the administration site : the user would first click on "Continent" in the first screen ; it would then show a list of continents ; clicking on a continent would show a list of the countries associated to this continent ; finally, the user could edit a Country form, displaying some inline region forms to allow adding country and region on the same page.
I hope this concrete exemple make it easier to understand...
U can use GenericForeginKey() to solve the limitation problem.
Ref https://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/
The easiest way is to use Country as a start point: in its form page you could inline edit Region as well as force staff to choose from existed Continents or add new one by clicking +.
You could also modify ModelAdmin to open Country page and populate pk of a saved Continent to it, which is more difficult. Check this post also.
Furthermore, it's possible to mimic a ForeignKey pointing to A in C, and inline editing both B and C in A, just as GenericForeignKey does. However, it's hard and overkilled for most usages...
I want to add, some fields depending on others. I have city and country model. I can include country as foreign key in city model. And then if I will add both city and country in another model ( say content) then will it be just like dependent selectboxes? like cities will be shown based on selected country via ajax? If not then what is correct way? and also is there a way to add city on the spot while adding main content data if city is not already on list?
So are above possible by using django admin or is it that django don't give? If not then how can it be done in django autogenerated admin?
You can do exactly what you ask using django-smart-selects
Hope that helps...
I can include country as foreign key in city model
This seems like a good idea.
And then if I will add both city and country in another model ( say content) then will it be just like dependent selectboxes? like cities will be shown based on selected country via ajax?
No, it will not get filtered automatically, you will need to write that code yourself. Both in admin and on frontend.
and also is there a way to add city on the spot while adding main content data if city is not already on list?
You will get this in the admin area.
Go ahead and start doing it, and when you run into specific problems, post them here if you can't solve it. Also read the Django docs, it is pretty elaborate on the topic of models.
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.