Implement a typeahead for manytomany field - django

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.

Related

How to create separate form widgets for ModelMultipleChoiceField in Django

I have a ManyToMany relationship between two Django Models: Team and Member. A single Member can be part of multiple teams. I am able to successfully bind a Form class to a CreateView, and, using the standard ModelMultipleChoiceField,can successfully save the Form using the save_m2m method.
However, the default widget for the field is not suitable for my user experience. Instead of using a picklist, I would like to create a separate select box for each number of selectable Members per team. For example, if the Team can have 7 Members, I would like to show 7 select boxes, rather than one pick list that a user selects 7 different objects from.
I understand I may not get a complete answer, but would appreciate any pointers on if I should be looking into overriding the Field with a custom MultiWidget, or if using an inline formset might be a more appropriate route. Or, something else...
At the end of the day, I ultimately decided to stick with a single ModelForm for the Create Team form. Within this ModelForm, I dynamically create the number of fields I will need for the number of Member per team, and have created a function within the form that yields those fields.
I perform validation on those fields within the generic clean method of the ModelForm. I use the yield method within the Template to control displaying the individual fields.
I am sure it is not the most Django-y approach, as many people seem to use inlineformsets in theory, but this worked and the code does not seem overly hacky or unmaintainable.

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

Django Model field as formfield in templates

I guess this is a simple question, but I am not being able to find a definitive answer whether this is possible or not.
I have a Model object that is being passed to my template during rendering. While I use most of the Model fields to show their values, I came across a need to show a few of them as formfields (with their respective HTML element like rendered via ModelForms). Can ModelForm class usage be avoided here and simply use the Model's field to be rendered as formfield directly from template?
an example:
class MyModel(models.Model):
month = models.PositiveIntegerField(_('Month'), choices=MONTH_CHOICES)
year = models.PositiveIntegerField(_('Year'))
Now in template based on the above Model example I want to show the year as a value but month as a dropdown with selected current value?
Any pointer appreciated!
Use case:
Imagine a generic list_view like case where you are rendering a queryset. What you have there is object per iteration, however on the list view you want to allow your users to perform some quick editing of few object attributes to avoid forcing them to go to full edit mode (maybe via simple ajax).
No.
As canvassed in the comments, this is not possible. A model, and its fields do not know about forms - that is what forms, and modelforms are for.
The right way to do this is to wrap your model objects with a modelform, which you can adjust (using inheritance) to do whatever you want with the non-form fields, if you wish to make this more transparent.
Creating modelforms in the template means that you have nowhere to process the forms in django. Apparently you want to interface them with some kind of ajax interface, so perhaps this is not a problem for you, but in any case, I fail to see how it would be easier than simply creating the modelforms in the view.

How customize fields in the Django admin interface

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/

Django inline formset with child of a child

I have three models:
Variable, which has Group as a foreign key
Group, which has Set as a foreign key
Set
I want to create a form that lets user create a new Set based on existing Set. Only things that user is able to modify are the Set name and Variable values.
This has given me an idea of using inlineformset_factory to access models' children. Sadly, all the examples I found go only one layer down.
Is there any way to access the Variables of a Set through inlineformset_factory?
If not - what is a good way to accomplish my goal?
Thank you.
From my experience, it is not worth hacking the existing inlineformset_factory to achieve much beyond what it is meant to do.
You are better off writing a custom view with the qs you want to use and create unique ids for each form element, to identify its parent.