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
Related
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 have a model with ManyToManyField:
class WordList(models.Model):
words = models.ManyToManyField('Word')
These WordList objects should be created from admin, by choosing words (The size of WordList can be different, but, in general, there are should be around 10-20 words in every WordList) By default, Django admin use MultipleChoiceField to render control for ManyToMany field.
There are two problems with it.
The number of Word objects is around 100000, and when I try to edit the WordList object in admin - it takes about 10 seconds on my dev server to load the page. Obviously, almost all time is taken by SELECT * FROM "app_word". That is bad, I am want to speed up it at least to 1-3 seconds.
This is mostly design problem, but the issue reason connected with such large amount of objects. It is hard to find a Word in a drop-down list. I am tried to use [django-easy-select2][https://github.com/asyncee/django-easy-select2] which uses select2.js to add search to ModelChoiceField, but it works really slow and browser starting to eat all RAM and CPU.
Please help. Thank you.
P.S. I am using PostgreSQL
Please try this widget: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.filter_horizontal.
According to the doc:
By default, a ManyToManyField is displayed in the admin site with a
select multiple. However, multiple-select boxes can be difficult to
use when selecting many items. Adding a ManyToManyField to this list
will instead use a nifty unobtrusive JavaScript “filter” interface
that allows searching within the options.
In your code you would do this:
class WordListAdmin(admin.ModelAdmin):
filter_horizontal = ('words',)
I'm currently using Django 1.5.1 and using a custom user as described in the official documentation. I realized everything is stored under one table, the auth_user one.
My question is, why is it better to have everything in one big table, instead of having 2 tables like it used to be prior to 1.5 by using a user_profile table for all additional data? It seems smarter the way it used to be, in case we want to add 20 new fields for information about the user, it is weird to have everything in auth_user.
In my case, for now I have class MyUser(AbstractUser) with 2 additional fields gender and date_of_birth, so it's all good with this, but now I would like to have many other information (text fields) like "favorite movies", "favorite books", "hobbies", "5 things I could not live without", etc. etc., to have way more information about my user. So I was just wondering if I should put that under MyUser class, or should I define a UserProfile one? And why?
Thanks!
When you have it all in one table, then database access is faster. With the old way you had to join on the auxiliary table to get all the information of the user.
Usually when you see a One-to-One relation, it would be better just to merge them in one table.
But the new custom User model solves also another problem, which is what atributes a User should have? What attributes are essential for your application? Is an email required? Should the email be also the username with which a user logs in?
You couldn't do these stuff before this feature was introduced.
Regarding your question about where to put additional user information like "hobbies" and such, it really depends on how often you will query/need this attributes. Are they gonna be only on the user's profile page? Well then you could have them in a seperate table and there wouldn't be much problem or performance hit. Otherwise prefer to store them on the same table as the User.
I'd like to add a "user" column to the list_filters for a Django modeladmin. The model's user column only contains a dozen unique users, however, I have thousands of users, causing the select field to be rendering with thousands of options, making it unusable.
How would I make the select only show the users that are actually used by my model, or at least use some other widget for rendering the select, so the user doesn't have to scroll through thousands of options?
Your question is a little confusing. At first glance, you want to limit what items show up as available filters on the changelist Filters sidebar, but then you go on to talk about selects, which seems to imply you're talking about limiting the options for a field on your change form.
If the latter is the case, #kgr's answer is appropriate, however, if you're asking about the former, see my question and answer regarding a similar thing here on SO
I'm wondering what the best approach to take here is. I've got a form that people use to register for a class and a lot of times the manager of a company will register multiple people for the class at the same time. Presently, they'd have to go through the registration process multiple times and resubmit the form once for every person they want to register.
What I want to do is give the user a form that has a single <input/> for one person to register with, along with all the other fields they'll need to fill out (Email, phone number, etc); if they want to add more people, they'll be able to press a button and a new <input/> will be generated. This part I know how to do, but I'm including it to best describe what I'm aiming to do.
The part I don't know how to approach is processing that data the form submits, I need some way of making a new row in the Registrant table for every <input/> that's added and include the same contact information (phone, email, etc) as the first row with that row. For the record, I'm using the Django framework for my back-end code.
What's the best approach here? Should it just POST the form x times for x people, or is there a less "brute force" way of handling this?
Django includes FormSet for dealing with exactly these challenges. Using a FormSet you can create multiple forms for creating or updating information. There's even possible to generate the FormSets from a Model. http://docs.djangoproject.com/en/dev/topics/forms/formsets/ and http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#id1 are great resources.
Now, for creating more forms on the fly, you need some javascript magic. I've done this on work projects using jQuery which made it a lot simpler. The basic idea is create a new form with the correct inputs and change the hidden metadata in the formset form so it will now how many forms to process. The admin implements this when using multiple inline forms so I suggest looking there for code as it is a bit tricky to get right.