How can I use model like a custom field - django

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

Related

how to correctly assign the through attribute in django?

I was just looking the documentation to be able to change the intermediate table but when I implement it, I get into trouble:
https://docs.djangoproject.com/en/2.0/topics/db/models/#extra-fields-on-many-to-many-relationships
The problem as such is that, although I can migrate the database and run the application, when I enter the administrator I do not visualize correctly the relationship of my models through the trough attribute (especially a field of my model called Tested).
Why does this happen and how can it be corrected?
This is by design. Django cannot automatically generate the widget for ManyToMany relations that use a through table because of the extra data needed (tested in your case). From Django docs:
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.
However, we still want to be able to edit that information inline. Fortunately, this is easy to do with inline admin models.
Your best bet is to create an inline admin model as explained in the docs.

Creating Model Relationships Via Forms

What is the defacto way of creating model relationships in Django via frontend forms.
For example a user signs up for service using a form, they start a quote.
In getting a quote they can select and add products to their quote specifying variable such as sizes in this process.
This is modelled with relevant User, Quote, Product models and relevant relationships.
I am trying to work out the best way that these are linked together by frontend forms and views.
Would I load into the quote form a hidden field for the related user_id for example, which I can then process manually to form the one-to-many relationship.
I am just wondering if this is something accounted for within forms or if I have to manually create the forms to achieve my goal.
This is one of the more complicated things to try and achieve but there are several things in Django which will help you.
You're going to need a ManyToMany field on the Quote model to link the Products to it.
This can be displayed in forms simply via a ModelMultipleChoiceField:
https://docs.djangoproject.com/en/dev/ref/forms/fields/#modelmultiplechoicefield
... which is just renders a basic multiple select list of existing products.
The interface you want probably looks more like an inline formset however. The complication here is that they are designed for ForeignKey relations rather than ManyToMany.
Under the covers, a ManyToMany relation is actually just two ForeignKey relations, via an intermediate 'through' model. We can exploit this to build an inline formset on the through model, see this answer:
https://stackoverflow.com/a/10999074/202168
You'll note the caveat in that answer, the inline rows won't know which Quote they belong to unless you override some code.
You may like to look at some helper apps which provide custom widgets for ManyToMany fields:
https://code.google.com/p/django-ajax-filtered-fields/
http://django-autocomplete-light.readthedocs.org/en/latest/

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: Best way to handle multiselect US state selection

What is the best way to include in a model to save multi-select US states? I need a user to select several states and save this data in a model (via form OR modelform ).
I tried LocalFlavor USStateField model it doesn't seem to work for me since I can't call it like a regular model.
This django app i've written may help you.

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/