Django admin: 'add' page, want to add multiple objects on one page - django

class Country(models.Model):
name = fields.CharField()
In admin, I want to display multiple forms for Country so I can add multiple different country objects at once. I don't know where to start? Please help, thanks.

I can't think of any way to do this inside the admin. The admin is a ready-made interface for editing single objects (and, optionally, multiple objects related to that object), but doesn't give you any way to edit multiple objects at once.
If you need this, write your own view using a formset.

My idea is you could extend the admin template change_form.html to display a formset, and have the 'add' url point to a view that would handle the page rendering. You will also need to override the url in your urls.py. Not the best but it would work.

Related

Can I add 2 or more different forms in the same template?

I am new to Django and was working on a page where I need to allow the user to place a bid on an item, leave a comment or add/remove this item from his watchlist
so I thought I would need to have a form for each of these tasks, but all I found about multiple forms was formsets and that's not what I need.
So my question is how can I have more than 1 form in a template or what are the alternative ways to do this?
Thanks in advance
As answered here:
Proper way to handle multiple forms on one page in Django
You can read the submit data to find out which form was filled out or you can have two different views for the two forms by giving them different URL's in the action.

Creating Pages for Django Admin add_form Inline Children

I have a node_user which extends auth.User in my django application. The node_user has many children. When creating a new node_user in the django admin I also provide access to node_users's child objects using the StackedInline sublclass.
However, I want to present the node_user children in a series of pages instead of a stacked one-page list. Is there anyway to spread the children out over pages? Does someone have an example? I think this would help user interaction but I'm unsure how to do it because I'm a newbie.
Thanks
angelo
Django doesn't do this out of the box sorry. You could write a custom admin view but you'd have some tricky decisions to make and coding to do regarding saving. I.e. does hitting 'next' lose changes you made on the first page? My approach to this problem would be to edit the node_user children through their own ModelAdmin - smart use of the list_filter and/or search_fields options (and perhaps list_editable) should make it pretty easy to edit them as a group.

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-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.)

Django Admin Template Overriding: Displaying checkboxselectmultiple widget

Have 2 tables Domain and Group having one to many relationship.
These tables have many to many relationship with User table
On the User admin interface I am rendering the Group and Domain as CheckboxSelectMultiple
widgets.
Is it possible to present this in a table form with 2 columns: Domain in one column and the list of groups belonging to the domain in the other column.
I want to override the fieldset template of the admin. However I am having difficulties knowing which methods/properties I can use with an AdminField.
Thanks
I'm not quite sure I 100% follow what you are trying to display.
AdminField is not documented unfortunately but its a short class, only 18 lines long so you can read it here.
I have a feeling you might be trying to step beyond what the admin allows you to do easily, once you are trying to combine more than two different models on the same page things can get a bit messy and you are soon in the business of customizing the admin by writing custom views and templates.
Am I correct in thinking you want to change the list of the objects? Rather than changing the editing/creating page?
I had similar problem and what I did is that I created new html pages and copied the same code from the Admin Template directory HTML pages to my template directory which will be overridden automatically, and then changed the HTML code to what I wanted to be. hope this is useful.