Create/update a sub-object in the Django admin edit dialog - django

I'd like to create/update a "sub-object" within an admin edit dialog.
I have a "CmsObject" model, which contains several "CmsPageItem" objects (currently there will be just one fixed CmsPageItem, but that will change in the future). Rather then letting a StackedInline widget control the layout, I would like to display one additional textarea field somewhere in the change_form.html page.
What would the recommended approach to extend the ModelAdmin dialog be?
I expect I need to push a formfield somewhere, or introduce new values in the template context?

Have you looked at TabularInlines? There is an example here: http://docs.djangoproject.com/en/dev/intro/tutorial02/

I've eventually settled to implement the whole view myself. For most simple objects, implementing the inlines (either with a custom template, or without) is good enough. In this situation I require more control, so I've overwritten the entire add_view and change_view completely.
FeinCMS also does this for it's editor window.

Related

Twitter Bootstrap's ".input-prepend" with a Django Form

I'm new to Django and have been using django-bootstrap-form. I've been pretty happy with it, but I don't believe it provides a way to format the output for ".input-prepend" as described in Twitter Bootstrap's docs.
I'm guessing I'm going to have to override my input field's widget for this particular field. I'm not sure of the best way to do this, though. Any help would be appreciated.
You'll have to edit the templates, adding an if clause for prepending and possibly two more for appending or both cases at once or handle this with a templatetag (or beter even, the widget render method).
Have a look what's there to get the hang of it.
For the field itself either create a custom formfield or create a widget by wrapping a basic widget.

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.

Django - Avoid saving unchanged object in admin

My admin.py seems to work very well thank you, but my user has the option of clicking "Save" even when nothing has changed. This sets the object's modified_date field which annoys me.
Is there a way to force a cancel when using "Save" in these circumstances to close a change form in the admin? Is it desirable to do so?
You could probably write a custom admin form and subclass it for every admin-editable entity where, in the save() it looks at all fields which aren't the last_modified_date (I'm assuming you've got this consistently named across your models) and if there are no changes, doesn't call super(YourAdminFormClassNamehere, self).save(*args, **kwargs) but if there are changes to any of those fields, it does.
(It's a weekend, else I'd probably add some example code. This should get you on a useful track, though.)

Django-admin with callable functions

Django-admin view allows only for changing the values of model objects. Is it, however, possible to configure or change in an easy way the admin view so that it starts exposing functions on objects? I'm not talking about the functions that can be introduced in the drop-down menu on top of the object list. What I mean is a direct access to functions on model objects?
You can add your own view to the admin site by adding an "^admin/..." url in your url conf. You can use this to extend the admin site relatively easy and expose model methods through your own view. See Creating Custom Admin Views here: http://www.djangobook.com/en/1.0/chapter17/ (and another approach and notes here: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#adding-views-to-admin-sites ).
To add this as a button in the model's "change form" in the admin site, override the change_form.html template for the required models (see https://docs.djangoproject.com/en/dev/ref/contrib/admin/#overriding-admin-templates ).
You will probably want to override the object-tools block, which holds the buttons in the top right side of the page. (In Django 1.3 you can extend the object-tools-items block instead, see: https://code.djangoproject.com/ticket/12694 )
(I am quite sure one can build a nice plugin/app that automatically adds object-tools to a model from a custom "object_tools" property in the ModelAdmin with a list of model methods. Let me know id you find something like this.)

Reusing Admin forms for user views in django?

Django is making very nice forms after creating a models.py and an admin.py.
How can I reuse these forms (with the extra nice handling of foreign keys and many-to-many fields) in my own views?
ModelForm does only generate "simple" forms. Where do I get the extra batteries?
I was actually able to replicate those green buttons in my forms by following the instructions on this page: http://www.hoboes.com/Mimsy/hacks/replicating-djangos-admin/
A stock ModelForm will do almost all of what the admin does (ForeignKeys will turn into a dropdown select, ManyToManyFields will turn into a multiple-select).
The main exception would be the little green plus buttons for adding a new entry. It would be pretty hard to make those generic, as they depend on a number of admin-specific things: knowing where to find an add page for the linked model; JS to popup a window, close it on submit, and update the parent page; etc. You can dig into the admin and figure out how it implements those extra niceties, but there is not going to be a simple way to drop them into your code.
The other nicety you might be wanting is the filter_horizontal or filter_vertical alternative UIs for a ManyToManyField. Those are implemented as ordinary form widgets, so the potential is there for reusing them in your own code, but I'm guessing it'll take some experimentation and customization to make it work properly.