I have a django model that has a postgres array field
ArrayField(blank=True, default=list, base_field=CICharField(max_length=200, blank=True))
There is also a corresponding admin page for this model where I can update field this in the regular case, but If for example I want to save the field as ['foo,bar', 'baz'] this is not possible since django seems to be splitting always by comma. I tried saving with "foo,bar",baz thinking csv rules but that didn't work out either. Is there any workaround to achive this in django admin ?
Related
Please, I am creating a to-do list app as my first django project.
My form is not valid because I am trying to insert DateTimeField in django using datetime-local.
I did research, I discovered I need to do a custom model so I parse the datetime-local field.
Pls guide me on how to create a custom model field.
I have client model which has a foreign key field to a country model.
So in Django admin, when I create a client and I select the country where this client belongs to. but the problem is the select list is too long(too many countries on this planet). Sometimes it takes just too long to get the one I need.
So I wonder if there is other widget in djano admin that provides a select-input-combo widget.
When I type in the input and it will filter out the right one for me to select.
Hope you can understand what I need here.
did you see raw_id_fields ?
you would do in admin.py something like:
class ClientAdmin(admin.ModelAdmin):
raw_id_fields = ("country",)
admin.site.register(Client, ClientAdmin)
then select widget will become something like:
Since Django 2.0 there is autocomplete_fields. From the documentation:
autocomplete_fields is a list of ForeignKey and/or ManyToManyField fields you would like to change to Select2 autocomplete inputs.
and
The Select2 input looks similar to the default input but comes with a search feature that loads the options asynchronously. This is faster and more user-friendly if the related model has many instances.
Note that you need to define search_fields in the related object's ModelAdmin since it is used by the widget.
Say I have a single form that has a field type and depending on what type the user inputs, the data is placed into a model/table. How do you do this with django forms?
Thanks!
In Django, a form for more than one model is called a FormSet.
I'm trying to implement a reusable form that users can download the source code in HTML which was site generated for them by the site. They can also create their forms using an interface where they get to choose the fields they want and rank them so the highest would appear first in the downloadable form.
They can then use the form in their sites to gather customer data and when the form gets submitted, the action parameter points back to our site so we can store the information on our servers.
Now, my question is, how can I save a Django Field object in my database? This is so I can store the forms that the users created, especially the Fields that they chose.
I need to store the Fields so that I can create a generic Django ModelForm and the users can save the Fields of that ModelForm in their downloadable forms. So when they submit back the information to us I can feed the information straight to the ModelForm (since the Fields would be recognizable) and do some further validation.
Also I have to add some basic javascript validations (like validation.js) for each field (must be present when the user downloads the form), and I know it will be easier if I can save the Fields themselves.
You could pickle the Field object:
import cPickle
field = forms.CharField()
dump = cPickle.dumps(field) # serializes field as an ASCII string
my_model.stored_field = dump
To unpickle:
field = cPickle.loads(my_model.stored_field)
http://docs.python.org/library/pickle.html
I have a many-to-many field on one of my models and a ModelForm to represent it. I have it in a template but it shows up as a multiple select field. I need it to show up as a CharField so the user can put in comma-delimited values. Is there any way to do this?
Take a look at the code in django.contrib.admin.widgets.ForeignKeyRawIdWidget to see how the admin's raw_id_fields are implemented and try specifying it as the widgets= kwarg when you define that field on your form.