adding code for a Django admin field - django

I have a tinymce textarea in my django admin and I need users to be able to upload images (via AJAX) that will be linked in this textarea.
That would be accomplished by adding an "Image Upload" button (it's already working) in the top of the "Content" textarea.
What's the recommended way of doing that?
I can think on 2 solutions:
extending change_form and replacing {% for fieldset in adminform %} for the actual fields... and when it's the content field, I add this value
dynamically adding this button with javascript (find out where the content field is and add a <div> before it)
A better solution, if possible, would be to override just this specific field in the admin templates. Is that possible? Or are there better solutions?
PS: this field is not part of the DB (it just uploads one or more images, saves it to the storage and returns a link that will be included on the tinymce).

maybe you could use this
formfield_overrides from the djangodocs
if not, create a subclass of modelform and overide the form of admin with ModelAdmin.form.

Related

Flask-Admin: Adding a WYSIWYG editor to an inline model?

Pretty much as the subject has it: I have a Flask-Admin site, and would like to add TinyMCE or CKEditor to a textarea in an inline model.
There are various instructions for adding CKEditor to Flask-Admin in general, e.g. Flask-Admin and CKEditor WYSIWYG textarea integration and Getting CKEditor to work with Flask-Admin. I've followed these to create the WTForms widget and field. The inline models don't use edit.html, for pulling in the JS, but I can add it to the master template, though this is not ideal.
But the main issue is how to attach the WTForms field to the Flask-Admin model. The existing instructions have you set a form_overrides = dict(fieldname=CKTextAreaField). But that doesn't work inside inline_models. How do I accomplish this?
I had this issue a few days ago and managed to solve it by adding this to my view class.
class CustomView(ModelView):
extra_js = ['//cdn.ckeditor.com/4.6.0/standard/ckeditor.js']
inline_models=[ ( YourModel, dict(
form_overrides={
'FieldName':CKTextAreaField
}
))]
Assuming that you followed the flask admin docs tutorial for CKEditor.
inline_models consist of (model, options) pairs. The "options" object is the key here: it accepts most of the form_* attributes that ModelView has.

Django 1.7 admin allow_tags issue

I am building a quiz application and I have a Question model. I want to allow the Question model to handle html tags. By using change_form.html and specifying {{ content|safe }} I was able to allow the add and edit Question pages to render the html tags. In my model I created a custom field that had allow_tags set to True which enabled me to render the Question correctly on the admin list pages. The issue now I am facing is that the breadcrumbs that appear in the admin and also the Recent Activity Widget on the dashboard shows questions and its showing away the html tags. How can I override these to handle the html tags ?

How to customize the "Clear" checkbox for a models.ImageField in django Admin page?

i have a Model for a user profile in my django app that has a models.ImageField and i have an ModelAdmin for it
when a user uploads an image , in the admin page , when i go in that user's Customize page , in the ImageField section , there is the url of uploaded image and a checkbox named "Clear" and a button for updating the image. how can i change the text of that checkbox ? for example i want it to have the text "Delete" instead of "Clear"
It seems like "Clear" is hardcode.
So either you create a custom widget simply like that:
class MyClearableFileInput(ClearableFileInput):
clear_checkbox_label = ugettext_lazy('Delete')
And assign it to your form field like that
MyForm(forms.Form):
myfile=ImageField(widget=MyClearableFileInput)
Or add overwrite it in your admin
class MyModelAdmin(admin.ModelAdmin):
formfield_overrides = {
models.ImageField: {'widget': MyClearableFileInput},
}
Or you use the translation mechanisms to translate Clear into Delete. Django translation is described in the docs pretty well.
I personally, just think that it is quite some overhead for your problem, unless you are using translations anyway. I would clearly recommend the custom widget - the addtional code is really minimal.

How to modify the way a ForeignKey field is rendered in a Django admin page to avoid browser crash?

I have a Customer model which contains a ForeignKey to a Contact model.
I have over 100,000 contacts in my DB and when I load the admin page for a specific customer, the dropdown menu for the contact is getting populated with ALL of the contacts in the database. This has recently, due to its shear length, started causing my Firefox to crash while the admin page is loading.
Is there a way to either:
replace the field with an integer
field I can manually modify to the
contact ID when necessary
replace the dropdown menu with some
alternative input method which won't
crash the browser
remove this input
from the Customer admin page
altogether
Thanks!
You can do any of the either of things you want to.
Simplest solution is the exclude the field from the admin. Just say so in the admin class.
You can change the field to be text input and display it's primary key rather than the item itself, by including it in the raw_id_fields of the admin class.
You can also replace the standard dropdown widget with the Auto complete text field input. Use the implemented widget, or other equivalents. - This is probably the solution you like the best.
You can also override the formfield_for_foreignkey method on the Admin model to customize the queryset that gets displayed in the foreign-key dropdown. You may want to checkout my implementation for displaying only the current User's (or subdomain's) added entities.
Sounds like specifying the contact field in raw_id_fields in your admin.py entry for the relevant model would sort you out. Docs are here.
PS. Surprised (but not that surprised) that FF gives out before your database server tanks...

How to extend an ImageField in my own django admin template?

I would like to make a custom admin page for one of my application with django. I've created a change_form.html and fieldset.html in admin/myapp/mymodel of my template folder. I am now able to customize the page.
mymodel has an ImageField and I would like to display this image on the page. I guess this is possible because the ImageField shows a link to the image on the page.
I am trying to modify the fieldset.html but unfortunately I don't know how to access the url in order to put in an img html tag. {{field.field.field}} shows an ImageField object but how to access the current value for this field?
Thanks in advance
<img src="{{field.field.field.url}}">
On a related note, instead of having to do all that, you could use django-form-utils that will provide you a thumbnail included display and clear-able file field, for free.
The current object can be accessed in a custom django admin template with {{original}}
It solves my problem