How customize fields in the Django admin interface - django

I have a model in my Django project with a member which is a char field. Basically data in this field will be entered as comma-separated values.
Without a long-winded explanation of what the overall goal of this is, basically rather than having the admin interface use a simple text field, I'd rather have have some custom HTML for the form so I can just use checkboxes and assemble the values of the checked boxes into a CSV string myself once the form is submitted.
Most of the django customization I was able to find on Google didn't answer my particular problem.

If I understand your question correctly I think you want to search for writing custom widgets. Perhaps start here: http://docs.djangoproject.com/en/dev/topics/forms/

Related

How can I use model like a custom field

Recently I've been developing a Django website, which includes the owner being able to add content with descriptions etc.
The problem I'm having is: How can I make the fields support multiple languages? (3 in this case)
The approach I tried was: Creating a model with 3 text fields, have my content model take that model as a foreign key. This sort of works, but now I would have to create all the descriptions first, separately, before creating the actual object it is being used by. This is, in my opinion, a bad idea.
What I would like to be able to do, is to have 3 text fields in the model which is actually using those 3 text fields' admin page, but without actually having 3 text fields in that model.
Using inlines would work, but I'd have to make my multilanguage textfield model have a foreign key to my content model, instead of the other way. This would mean the multilanguage model works for only other model type.
So, to clear the question up:
How can I have a TextField and a CharField support multiple languages?
How can I show the ForeignKey's target model's creation widget in it's owner's admin page?
How can I use inlines, without locking the inline to just one model type?
How can I make a model act like a field?
How can I write a custom TextField?
Answering any of those will be enough for me to solve my problem.
Thanks.
There is too many questions and the docs is at your reach... I'll just answer the easiest one you should have search for by yourself.
How can I have a TextField and a CharField support multiple languages?
You should have a look to i18n here
How can I write a custom TextField?
Have a look to custom Fields

Implement a typeahead for manytomany field

I am looking for a robust solution to implement a typeahead (Twitter typeahead) for a manytomany field. Basically, something identical to the tag input field here in StackOverflow.
The default widget for manytomany is a multiselect. However, since I want the user to provide new values, I need to use a inputText widget. My question is, what would be the best way to implement this functionality so that I can later pass an array of models instances to a cleaning stage?
In my cleaning stage I plan on doing a loop through the elements to check if they exist in the db, create them if they don't and apply validators to each elements.
My initial intuition was to use a hidden field that would receive the actual fields from the typeahead via javascript manipulation. Thus the input field would not be part of the model, just serve as an input box for the user.
Why reinvent the wheel. You can simply use django-taggit together with selectize.js. By using both of them, you don't even need any customization.

Django form with autocomplete functionality

I am working on a django project with complex forms. In one of my form fields I need following functionality...... Its the text field. As the user starts typing the value the suggestions from existing database should appear in dropdown. Can anyone help me out with this ? Just similar to autocomplete but able to add new values.
This is going to be something in the JQuery/AJAX side of things, not Django. I would read up on the autocomplete functions of JQuery and use AJAX to call your DJango code and receive a populated list, which then displays to the user.
JQuery Autocomplete - Custom Data
If you don't want to deal with JavaScript, you can use a django application called django-autocomplete-light.
You can learn more about it (and get it) here: https://github.com/yourlabs/django-autocomplete-light

Django, custom fields, to_python, and displaying serialized text as-is in Django admin

I have a complex object that I'm storing serialized in a text field. For most purposes, I want the object pulled from the database to be that complex object. However, when I'm editing it in a form, I just want to see that serialized text in the field.
I tried using the value_to_string function but it appears as if it isn't being called at all when editing the record from with admin.
What do I do so that the raw serialized text shows up in the admin text field?
Since the admin already uses the value of your model field, one option would be re-serializing it for editing...
See also formfield_overrides:
This provides a quick-and-dirty way to override some of the Field
options for use in the admin. formfield_overrides is a dictionary
mapping a field class to a dict of arguments to pass to the field at
construction time.
I'm not sure if it's the same problem, but I figured out a way to change the field value in the admin form before displaying it. I explained how to use a custom widget and an custom admin form in this answer.
Note that the custom widget only helps you to display the value in a different format. It won't parse the input value back to an object, although I believe that would be possible too.

Dynamically Update a Form in Django AFTER Initial Creation/Display

Is there away to dynamically modify and validate a Django form AFTER it's been created and displayed.(I have found a few snippets that show dynamic form creation, but these require that the dynamic fields are known/defined prior to creating the form.) My requirement is different.
Use Case:
I have a form where I want to display and validate additional input fields based on the selection from a dropdown on the initial form. Based on the selection additional fields are 1) added and 2)must then be validated with appropriate error handling.
(In case you are wondering the data elements to be added are a set of name/value pairs stored in csv format in the model, but when displayed they are shown as separate input fields - the input data will be converted to a csv string prior to saving).
I got this partially working using ajax to dynamically add the additional fields to a template, but have not found a way to validate these new fields. I'm not sure if this is a workable approach. I could probably do this in the browser using javascript, but would prefer a Django/server side solution.
OK - So none responded to my question so far. However I did a bit more googling and found an excellent article that describe the solution I was looking for. Major Kudo's to the author of this blog.
Create a Django Dynamic Form with JQuery - Dynamic Field Addition and Removal