How to make a drop-down that show images in Django Admin? - django

I spent two hours trying to find out how to customize select2 widget in Django admin to implement this simple feature.
I have a model (Article) that contains many other objects (Pictures). Every Picture has a set of FileFields representing the original image file, the thumbnail, post-processed (using Pillow) preview and some meta stuff.
What I'm going to achieve is to be able to upload new images on the Article change page, and select/deselect them in the fancy Select2 dropdown widget.
How can I do that? Should I use ImageField instead of FileField? Should I use inlines and through for ManyToMany relation between Article and Pictures? Should I make my own widget? How do Django admin uses Select2 and how could I pass parameters like "templateResult" to be able to change the look of dropdown items?
Thank you in advance.

Related

Is there a way to have custom content with mezzanie/Django

I'm new using Mezzanine and figured out how to set-up pages where I can manage content from Admin page.
But I have static pages, where I want to store some content and being able to control that content from Admin page.
Is this something I can do with Mezzanine?
I imagine that I need to create a model with richtext field add that model to admin interface and than somehow access to that model through templatage.
But any exact example would greatly appreciated.
Thanks!
See the docs on creating custom content types. The primary approach is to subclass the Page model and add your custom fields.
Alternatively, if your custom content is conceptually independent from your pages, it might make sense to create independent models with relational fields to the RichTextPage model and edit them through inlines.
Note that the mezzanine docs on custom content types and the django docs on inlines use the same author/book example so you can easily compare the two strategies.

Django-cms Edit model placeholder before model is added

Is there a good way to implement editing of a PlaceholderField before the model is added to an app?
I have an app with an apphook, and at present one has to create the model in the admin interface, save it (causing it to appear on the website), and then add the CMS content to the already-visible rendering of the model. Clearly this isn't very good.
Ideally I would like to be able to create the Placeholder content 'inline' along with the rest of the model.
What would be the best way to go about this? Is there an existing method, or do I hack the admin interface and the model's save method using the CMS API?
Thanks

I want one choice to effect two fields in admin

I am subclassing a model from Mezzanine, a django app.
In the admin, I want to have a choice list. The choice is tied to an image from a fixed list of images which will be displayed in a blog post.
So, the admin would have a nice little thumbnail next to the choice after choosing from the list.
I have looked into subclassing or extending a django field, but this is above my head (not totally.)
FileBrowseField is the closest fit I've found.
I would consider any path offered. I like a challenge and I have the time. I'm not looking to have the blog writer upload an image, but the images are with the choices already.
Again, I would like to include the image in the admin page.
This is going to require knowledge of Javascript as well as Django's admin system and template language. The core components are a custom admin.py for your custom model.py, an admin widget for choosing your image, and template changes for both the end-user and admin interface. A static list of images can be included as choices on a CharField. If the value of the choices key-value pair is a url to an image, you can then write a thumbnail of that to your template.
Alternatively, you could create an Image model that has a ForeignKey relation to your blog post (or vice versa, depending on your goal) and use the FileBrowseField as a field on that Image.

Django admin: two change lists on the same page

I am trying to make an overview page for one of my models I have read through all of http://www.djangobook.com/en/1.0/.chapter17/ and understand how I can add my own custom views for a model to the django admin.
What I am currently trying to do is add multiple filtered change lists (presenting some child models) onto my "overview" page for the model. In these I would like to be able to make use of some of the admin features such as editable fields or actions.
Does anyone have some pointers on how I can best get started with this.
Check this out
He created a proxy model for the original model to avoid the conflict. You can create a new model admin or inherit the old ModelAdmin(as shown in the link)

Django form to enter/save html to database

I'm in my first week of Django development and am working on an admin page that will let me write some quick html using TinyMCE and then save it to the database. I don't need to display this web page on the site or add it to urls.py, etc. The html snippet will be loaded from the database and used in a view function.
I've read in "Practical Django Projects" how to integrate TinyMCE, so my question is more concerned with the best approach for the form itself. Specifically:
1. Is there a built-in form like flatpage that works well for this? I only need one field in the form for the html.
2. How do I save the form's text after it's entered?
I created a model with a JSONField to save the html in, but I'm not clear on what to do next. Thanks.
here is the documentation for Django Flatpages App, maybe you serve.
I ended up using the ModelAdmin class to get what I wanted. I created a new model in models.py and then used ModelAdmin to enable an admin-editable form for the model's data.
Hope this helps someone.