Putting 2 fields in the same column in the Django Admin? - django

Two of my model's fields are "title" and "summary". Right now, "title" is the first field in the ModelAdmin's list_display, which makes it link to the change page. I have some other fields in list_display as well.
I'd like to make the admin change list page display "summary" under "title" in plain, unlinked text, in the same column as "title". Is this possible? I'm using Django 1.1.
Thanks

Kind of. You can setup your own custom list_display objects to use. So for example, in your case you may do something like so:
def title_and_summary(obj):
return "%s %s" % (obj.title, obj.summary)
Then within your admin class:
class MyAdmin(admin.ModelAdmin):
list_display = (title_and_summary,)
More information can be found on the list_display documentation.

Related

Django admin: Change the displayed value of a key

i have a model that includes a key "service_id" and the values stored in database are like "10001", i want to add a method to admin page that instead of displaying the id it displays a custom value like "Car Wash".
I think you need to create a custom field to be added in your list_display
from django.contrib import admin
#admin.register(MyModelClass)
class MyModelClassAdmin(admin.ModelAdmin):
list_display = ('my_field')
def my_field(self, obj):
return obj.description().title()
my_field.short_description = "My field"
You can change the value returned by the my_field method as you prefer (take into consideration that you can use the obj instance).
Create an Admin Class following
class ModelclassNameAdmin(admin.ModelAdmin):
list_display = ('service__name', [other fields])
in it for list display use double underscore __ for accessing field inside foreign key like service__name
and while registering the model add admin as second argument.
admin.site.register(ModelclassName, ModelclassNameAdmin)

django admin list_display textfield

I've a Textfield defined in model.py
In the changelist data are shown as single line instead
in the change view of the object, the data are rendered in a:
vLargeTextField
the break lines are mantained as in the user input.
es.
hi,
nice to meet you,
I need a break
Is there something special to allow the list_display to show the data as in the change view?
You can render breaks as html (the linebreaks templatetag make it easy, or surround with the html pre tag i.e. <pre>... (your_text) ...</pre>) and set the allow_tags property to True for this field within your admin class definition.
admin.py
class CustomAdmin(admin.ModelAdmin):
model = YourModel
list_display = ['your_large_text_field__custom_rendering']
def your_large_text_field__custom_rendering(self, obj):
return "<pre>%s</pre>" % (obj.your_large_text_field,)
your_large_text_field__custom_rendering.allow_tags = True
admin.site.register(YourModel, CustomAdmin)

Django labels for custom admin fields on inline

I'm new both here and to Django, and I have a doubt about custom fields:
I'm using custom models in my admin, but I would like to change the label that shows on the tabular inline.
My model is like this:
ModelA(Model):
name = Model.charField(80)
(...)
ModelB(Model):
modelA = ModelA
(...)
then in my admin I use:
def name_modelA(self):
return ("%s" % self.modelA.name)
class ModelBInlne(TabularInline):
(...)
fields=('field1','field2',...,name_modelA)
(...)
thing is, on the admin page the label for the name comes out "name_modelA", I'd like to change it to "name". Is it possible?
Hope I was clear enough.
I guess you will get what you want with verbose field names? That controls the label for editable fields in the admin interface.
For the tabular interface, I'd say something along the following lines should do:
class ModelAInline(TabularInline):
model = ModelA
class ModelAAdmin(ModelAdmin):
inlines = [ ModelAInline, ]
This makes your ModelA editable inline. I'm not exactly sure how you would like to have it displayed in your ModelB, but perhaps you could look through the ModelAdmin docs and use something from there?

ManyToManyField widget in a django admin change list?

In the change list of the django admin, I want to use list_editable to display a django-autocomplete widget for a ManyToManyField.
I found something similar here: list_editable and widgets
Normally including a ManyToManyField in list_display raises an ImproperlyConfigured exception, eg:
""'BookAdmin.list_display[2]', 'author' is a ManyToManyField which is not supported."
I (perhaps unwisely) removed 3 lines from contrib/admin/validate.py to bypass the exception. :)
I now have it spitting out the following, which is close(?) but no cigar.
<django.db.models.fields.related.ManyRelatedManager object at 0x1032a85d0>
Any thoughts on how to proceed? Is there a better way of doing this?
Here's what I have at the moment: (AuthorAutocomplete is working fine in the regular admin form)
class AuthorAutocomplete(AutocompleteSettings):
search_fields = ('first_name', 'last_name')
class BookAdmin(AutocompleteAdmin, admin.ModelAdmin):
def get_changelist_form(self, request, **kwargs):
kwargs.setdefault('form', AuthorAutocompleteForm)
return super(BookAdmin, self).get_changelist_form(request, **kwargs)
list_display = ['isbn', 'title', 'author', 'publisher']
#...
class AuthorAutocompleteForm(forms.ModelForm):
class Meta:
model = Book
author = AuthorAutocomplete
Thanks!
To get the values for a ManyToMany field in your own code so you can display their values you can do the following. I'm using list_display as an example.
class TestAdmin(admin.ModelAdmin):
def get_some_value(self):
return ", " . join([x.__str__() for x in self.manytomany_field.all()])
get_some_value.short_description = 'Title' # sets column header title
list_display = ('get_some_value',) # define Model's fields to be presented on admin changelist
where manytomany_field is the name you gave your manytomany_field and get_some_value is a method within the class which is assigned a short_description.
Silly me. I thought that William's manytomany_field was a built-in ModelAdmin object. So I ran his code as is (after putting in the missing parenthesis after join).
Strange to say, it ran without producing an error message. But (None) appeared when I was supposed to get values. And I thought it was strange that I couldn't find anything when I Googled "django model.Admin.manytomany_field". Hah!
So eventually I realized that I was to put the NAME of the many-to-many field in place of manytomany_field. It works!

Django admin raw_id_fields table display

Django raw_id_fields don't display tables the way I expect, am I doing something wrong? I'm trying to use a raw_id_fields widget to edit a ForeignKey field as follows:
#models.py
class OrderLine(models.Model):
product = ForeignKey('SternProduct')
class SternProduct(models.Model):
def __unicode__(self): return self.product_num
product_num = models.CharField(max_length=255)
#admin.py
#import and register stuff
class OrderLineAdmin(admin.ModelAdmin):
raw_id_fields=('product')
I get the little textbox and magnifier widget as expected, but clicking the magnifier gives me this:
flickr.com/photos/28928816#N00/5244376512/sizes/o/in/photostream/
(sorry, can't post more than one hyperlink apparently)
I thought I would get something closer to the changelist page c/w columns, filters and search fields. In fact, that's apparently what others get.
Any thoughts about how to enable the more featureful widget?
Ah, OK, this should have been obvious, but it isn't explained in the Django docs. The list that appears in the raw_id_fields popup uses the same options as the admin object for the referenced model. So in my example, to get a nice looking popup I needed to create a SternProductAdmin object as follows:
class SternProductAdmin(admin.ModelAdmin):
list_display = ('__unicode__', 'drawing_number', 'drawing_revision',)
list_filter = ('drawing_number',)
search_fields = ('drawing_number',)
actions = None
Hopefully this will help others in the future.