Conditional field rendering django admin - python

I am new to Django. I would like to show some fields in the Django Admin only if a specific value has been selected on a dropdown.
For instance, I have a question model. I would like to let the user select the type of question. If it is a multiple choice question, I would like to show fields to let the user fill the possible answers.
I have found this link but it seems that it is not done for the Django Admin.
How should I proceed to achieve what I need ?

If the object already exists (meaning it's not a creation form) I think this could help you: ModelAdmin.get_fields().

Related

Connecting django multiple choice field with textinputs

In django models, I used models.MultipleChoiceField but i want that those variables in the set of choices to be linked to a textinput which will be saved into the django admin. So the user selects the choice then keys in a textbox and saves for that choice is that possible to implement?
I think you can follow this link
If you want to "those variables in the set of choices to be linked to a textinput", it will be use forms.ChoiceField in forms.Form.
it will be nicely if you posting your code, the better we may know what you want.

share button for own site Django

I want to implement a ''share on my profile'' button on my page but I have a hard time how to do so. When I try to find something like that (google/here) the results are "how to share something from your site on twitter/facebook". If there is a similar question please share the link I was not able to find something.
So i have a site where users have a profile where the liked content is displayed and I would like to give the user the option to share a Post on his own profile with a personal comment(like FB/twitter does).
My problem is that I don't know how to implement this into my models. If I want to save the "shared" post in a QuerySet in the UserProfile model I don't know where to save the comment for each shared post. If I make an extra table for all the shared posts with a extra form combined its a total mess since each post is saved individually and I don't know how to combine the existing image in to the form where the user writes his comment .
Can anybody tell me in which direction I have to walk? Feeling a bit lost on this one.
You don't need to make an extra table, django will do it for you, if you'll use ManyToManyField in your Post model.
See this docs https://docs.djangoproject.com/en/1.10/ref/models/fields/#manytomanyfield

Create View-Only Django Admin Group

How can I make a group in the Django admin panel that can only view the model? I already have one group that can edit, delete and add objects, but I would like to create another one that can only view, search and filter.
Thank you in advance! Would be happy to answer any questions you may have!

How to code in openerp so that user can create his fields?

I have been developing modules in OpenERP-7 using Python on Ubuntu-12.04. I want to give my users a feature by which they will have the ability to create what ever fields they want to . Like they will set the name, data_type etc for the field and then on click , this field will be created. I dont have any idea how this will be implemented. I have set up mind to create a button that will call a function and it will create a new field according to the details entered by the user . Is this approach of mine is right or not? And will this work . ? Please guide me so that I can work smartly.
Hopes for suggestion
The user can add fields, models, can customize the views etc from client side. These are in Settings/Technical/Database Structure, here you can find the menus Fields, Models etc where the user can add fields. And the views can be customized in Settings/Technical/User Interface.

Improving Performance of Django ForeignKey Fields in Admin

By default, Django's admin renders ForeignKey fields in admin as a select field, listing every record in the foreign table as an option. In one admin-accessible model, I'm referencing the User model as a ForeignKey, and since I have thousands of users Django is populating the select with thousands of options. This is causing the admin page to load incredibly slowly, and the select is not very useful since it can take a while to scroll through thousands of options to find the one you want.
What's the best way to change the rendering of this field in order to improve page load and usability? I'd like the select field to be replaced with some sort of button to launch a search form popup, or a text field that searches keywords via Ajax to find the Id for the specific User they want to associate. Does admin have anything like this builtin, or would I have to write this from scratch?
Add raw_id_fields to your model to only show the ID instead of a dropdown.
You're right, Cerin, the cause of the slowdown is because Django is populating the <select> element with too many options. You might want to use an autocomplete element instead.
Interestingly, Django 2.0 has introduced a new feature on the admin site, called autocomplete_fields, which I think you will find useful in this case. It uses AJAX.
class ExampleAdmin(models.ModelAdmin):
autocomplete_fields = ['example_field_user']
You can use one of the few autocomplete apps for Django. Check them at Django Packages.
There's also django-extensions that have ForeignKeyAutocompleteAdmin that fit your needs pretty well.
Another option is to add readonly_fields instead of raw_id_fields

Categories

Resources