I'm using django autocomplete_light and have two models connected via one-to-many relationship. Model A has a ForeignKey field TAG to model B. It all works, but I can only select the existing Tag, it is not possible to automatically add new Tag, even though it is possible to freely type in the box.
How can I "intercept" validation and create the suitable database entry for tag in time?
You could use an add another popup like in django admin.
Here's a live example using this code. The design is not very very good but it demonstrates the point.
Related
I got a foreign key model with about 100,000 date.
It's too difficult to select the wanted data. So I'm wondering if there is a humanized way to add foreign key in django admin.
I have tried the raw_id_fields, it's useless.
this don't work.
If you have a raw_id_fields set on your source admin class, you can set the search_fields on the target admin model to be able to filter the result based on the set search_fields, i.e. you will get a search input field in the select list popup, see image below.
Alternatively, django-2.0 just merged a pull request providing support for using select2 widgets in the django admin, see pull request for the full list of supported fields: https://github.com/django/django/pull/6385
With this you can use the autocomplete_fields on a foreignkey field on your source admin class and as before set the search_fields on the target admin class.
Select2 is very handy to deal with such situation. You may check packages for Select2 integration with Django.
django-select2 and django-easy-select2 both are good option to consider.
I have a table with a foreign key. My problem is that ther's a lot of registers, so I need do that:
But all I've found was for the Admin Panel. Any idea for a custom form without admin?
Raw id, isn't needed any more, since there's autocomplete in the admin. This is also the solution for a model in the frontend. However, you should not bake it yourself, but just use Django Autocomplete Light. An excerpt from the tutorial:
Autocompletes are based on 3 moving parts:
widget compatible with the model field, does the initial rendering,
javascript widget initialization code, to trigger the autocomplete,
and a view used by the widget script to get results from.
I need to customise a through model of a many-to-many relationship, the customisation is subtle, because the user won't need do act manually, I try to explain myself better by explaining my use case with the following pseudo code:
RouterConfiguration
- vpn (many-to-many through VpnClient)
# other fields
VpnClient
- router: ForeignKey to RouterConfiguration
- vpn: ForeignKey to Vpn
- cert: ForeignKey to Cert
Vpn
# other fields
Cert
# (stores x509 certificates)
# other fields
The through model VpnClient has only one additional field, a ForeignKey to Cert, but I want VpnClient to automatically create a Cert instance without user interaction and until here there is no problem.
The problem comes in the Django Admin, because as far as I understood, it is not possible to use the classic many2many widget when using a through model:
When you specify an intermediary model using the through argument to a
ManyToManyField, the admin will not display a widget by default. This
is because each instance of that intermediary model requires more
information than could be displayed in a single widget, and the layout
required for multiple widgets will vary depending on the intermediate
model.
Reference: https://docs.djangoproject.com/en/1.10/ref/contrib/admin/#working-with-many-to-many-intermediary-models
But I don't want the user to insert any extra information. I just want to be able to control the model so it can perform a series of actions automatically.
So my question is: is it possible to have the classic admin widget with a custom through model? If there's no easy solution maybe I could try with a custom widget? Or maybe there is an alternative way to accomplish what I need?
PS: apparently there's a ticket for this use case: https://code.djangoproject.com/ticket/12203
I got a foreign key model with about 100,000 date.
It's too difficult to select the wanted data. So I'm wondering if there is a humanized way to add foreign key in django admin.
I have tried the raw_id_fields, it's useless.
this don't work.
If you have a raw_id_fields set on your source admin class, you can set the search_fields on the target admin model to be able to filter the result based on the set search_fields, i.e. you will get a search input field in the select list popup, see image below.
Alternatively, django-2.0 just merged a pull request providing support for using select2 widgets in the django admin, see pull request for the full list of supported fields: https://github.com/django/django/pull/6385
With this you can use the autocomplete_fields on a foreignkey field on your source admin class and as before set the search_fields on the target admin class.
Select2 is very handy to deal with such situation. You may check packages for Select2 integration with Django.
django-select2 and django-easy-select2 both are good option to consider.
We have a django application that is, at its core, a series of webpages with Forms which our users fill out in order. (We'll call the particular series of pages with forms on them a "flow".)
We will be white-labeling this application for a Partner -- the Partner will want to add some fields and even add some webpages with their own new Forms. This may result in a new order in which the Forms are filled out. (A new "flow", in addition to changes to existing Forms/Models or new Forms/Models.)
What is the best way to extend our existing, simple Forms-and-Models structure to use different Forms and Models depending on the running instance of the app (e.g. an environment variable)? Some things we thought about:
implement something like get_user_model for every Model and Form use in the app, which would look at the current environment
implement a more generic key-value store so that we're not bound by the current implementation's field types (i.e., have the data field name be part of the data as well)
a data model which tracks this particular environment's "flow" and which models it needs to use
subclass existing Models and Forms for each new white-label implementation
Model Field injection may be what you are looking for, take a look of this article
The approach boils down to three concepts:
Dynamically adding fields to model classes Ensuring Django’s model
system respects the new fields
Getting the load ordering correct for the above to work
Mezzanine has done a beautiful job implementing this model field injection with dynamic extra models via EXTRA_MODEL_FIELDS