I am a beginner with Django. In what situations are Django formsets used? (In real applications.)
Please give some examples.
According to the documentation:
A formset is a layer of abstraction to working with multiple forms on the same page. It can be best compared to a data grid.
So any time you want to have multiple instances of a particular form displayed on a page for creation or updating. An example of this might be a Phonebook. Each Form is a single entry in the phonebook, but a Formset of Phonbook forms will allow you to create or edit multiple entries in one go.
One case is when ModelA have a foreign key to ModelB. you can say that one ModelB has many ModelA, so lets say that ModelB is a Invoice and ModelA is the ProductItem, one invoice has one or many product items.
In the practice now you can make a basic formset like that: http://help.mailchimp.com/img/addafieldoption.jpg where you can add more product items to a invoice.
Related
I am rewriting some administration interface to django 2.2, currently using django autocomplete_fields admin feature. Simply said I have ModelAdmin object OrderAdmin, which has nested TabularInline ProductAdmin: variable-length table of products which might be added to order. Each of these ProductAdmin holders just contains ForeignKey to actual product class, with some other attributes.
Now I wonder: where does django store id - ForeignKey - of item selected with autocomplete field? It doesn't mark OPTION in selectbox as selected, and although there is suspicious hidden input field with #cashregisterproduct_set-0-id on page, it doesn't have any value. Or is there some special way how to access it? I was thinking about adding id to __str__ method of model and parsing, but thats just ugly.
Thanks for tip.
EDIT: to make it 100% clear, where from does django get ForeignKey of object selected through autoselect_field, when creating new object from ModelAdmin?
I got misguided thinking that this is managed by django. Selected data might be accessed by using select2 framework:
selected_value = $('.myselectbox').select2().val();
related: https://stackoverflow.com/a/47451658/16268461
Assume I have a model named MyModel and I have a Field Named field Now I want to add three more fields inside the prescription like one field_a , field_b and field_c .
Does Django Allow that in any way or we have to make another model for this purpose and then link with Foreign Key to MyModel?
Well I think it is an idea that could lead to some really hard to maintain code and should be well thought through.
That aside If you have a look at:
https://docs.wagtail.io/en/stable/topics/streamfield.html
It's a special field meant for cms applications that uses json in the field to model dynamic fields and validations for it.
Also if you run postgres this might help. https://docs.djangoproject.com/en/3.2/ref/contrib/postgres/fields/#jsonfield
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.
Well, I believe this is a general question.
Let's say I have a model, called Book, and another model called Author. And Author is the foreign key of Book.
Easy, ha?
On BookAdmin page, I want to show Author of Book. So In the list_display field I have:
list_display = ('book_name', 'author'.....)
Then I use django-debug-toolbar to check the number of queries executed when loading the page.
There are about 100+ queries have been executed. (I have 100 book entries on same page)
If I don't include 'author', there are only 5 queries (something like that).
I know that the extra queries come from foreign key.
My question is that is there anyway to reduce the number of queries being executed when we are dealing with foreign key?
I don't want to perform any complicate operation. I just want the call the unicode() method in that foreign key model.
Thanks in advance
Well, I found it out myself.
The way to do it is to override the queryset() in BookAdmin class.
Like:
def queryset(self, request):
return Book.objects.all().select_related('author')
That is it.
Django will load the Author in the same time it loads Book. So there will be only on database hit rather than over 100 hits.
I have a many to many link between Foos and Bars. I don't particularly like the multi select widget so was thinking of having a widget which allows selecting a single Bar and a button to add more choice fields.
From what I've been reading formsets may be the answer. Is it valid to have a formset made up of a form with only one choice field or is there a better way to get the behaviour I'm looking for?
I wouldn't worry about the quantity of fields in the form. If your 'child' model only has the one field, then I'd say it's perfectly valid to use an formset with single field forms in this application.
You should take a look at inline formsets, they should help with exactly what you need. Although I'm not 100% sure they work for M2M...
additional thought: If it doesn't inline forsmet doesn't work directly with M2M, you can just use a model formset, and manually save the relationship in your view after using formset.save(commit=False). docs: formset saving