Representing ManyToMany relation in the Admin Panel - django

I am trying to represent a ManyToMany relation in the Admin panel for the following models:
models.py
class Adress(models.Model):
address = models.TextField()
class Person(models.Model):
locations = models.ManyToManyField(Address)
admin.py
class PeronsAddressRelation(admin.TabularInline):
model = models.Person.locations.through
class PersonInline(admin.TabularInline):
model = models.Person
inlines = [PeronsAddressRelation]
The problem now is that I have many relations so when loading the admin edit page of a Person, it takes a lot of time until the Admin view builds the select box of PersonAddressRelation .
Is there anyway I can show inside of PersonAddressInline the name of the address without it being inside a select box? (just static text with a Delete option).

Check ModelAdmin.raw_id_fields. Using raw_id_fields will tell django admin not to create drop down menu.

Related

ManyToMany Field on Django Admin Interface

On Django Rest, I have a many to many relation for a field of material list. Instances of materials are added in a loan model. It works, to add through the admin site but the display is disintuitive: Instances are mixed with materials (instances are in parenthesis).
It's possible to have to separated list ? One to select materials and one other to add/remove instances linked to materials ?
The name that it shows in the list are the results of the __str__ method on your model. So if you want to display something else, you can alter the __str__ method. For example if this is a model Material with a name field, you can set it to:
class Material(models.Model):
name = models.CharField(max_length=128, unique=True)
def __str__(self):
return self.name
You can set the filter_horizontal attribute [Django-doc]:
from django.contrib import admin
#admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
# …
filter_horizontal = ('materials_field_name',)
this thus will use a widget with two lists where you can move to the right and the left.

Hiding a model field in Django admin 1.9

I have registered some models to display in the admin area, but I would like for some fields to be hidden.
As an example, I have a TeachingClasses model with a BooleanField named 'Status' that is set to True or False depending if the class is open or not. But that is set somewhere else in the app. There is no need to have that field displayed in the admin area when someone wants to create a new class to attend.
As such, is there a way to hide that field in the admin area?
I have tried adding this to the app admin.py file but it did nothing
from django.contrib import admin
class MyModelAdmin(admin.ModelAdmin):
class TeachingClasses:
exclude = ('Status',)
but it's not working?
Any clue if this is the right way?
My model:
class TeachingClasses(models.Model):
name = models.Charfield('Class Name',max_lenght=64)
[...]
status = models.BooleanField('Status',default=True)
What you did is not the correct syntax, you need:
class TeachingClassesAdmin(admin.ModelAdmin):
exclude = ('status',)
admin.site.register(TeachingClasses, TeachingClassesAdmin)
Django doc about how to use exclude.
In the admin.py:
class TeachingClassesAdmin(admin.ModelAdmin):
list_display = ('name',) # plus any other fields you'd like to display
admin.site.register(TeachingClasses, TeachingClassesAdmin)`

admin site doesn't show object fields

I have a model with a PointField from django.contrib.gis.db.models . This somehow doesn't let the admin site show the objects with a nice table of fields and values. Instead it displays one field named after the model name. The values are just a bunch of ' object'. With the name of the model. If I click the object I can edit it fine. It would be nice however to be able to filter and see the field values at the admin/ page itself.
Since PointField does not have a __unicode__ attribute, for the proper name to show up, you can register a new admin model object.
Now, in the admin's list_display,
class PointFieldAdmin(admin.ModelAdmin):
list_display = ('name', 'field_x', 'field_y', ...)
admin.register(PointField, PointFieldAdmin)
More on admin models registering here

Django admin: search for foreign key objects rather than <select>?

My model looks like this:
class Asset(models.Model):
serial_number = models.CharField(max_length=100, unique=True)
asset_tag = models.CharField(max_length=100, unique=True)
class WorkOrder(models.Model):
asset = models.ForeignKey(Asset)
Essentially, a work order is submitted and then an admin assigns an asset to the work order. The asset_tag field is a barcode that we can scan in. When editing the work order in the Django admin, by default the asset field is displayed as a <select> widget. What we want to be able to do is have a search field so that we can scan the asset tag and then search for the right asset in the DB to associate with the work order.
I know you can customize the Django admin foreign key to a hard coded query, but I can't figure out how to get it so it does a search based on a field on the admin page.
Did you take a look at raw_id_fields?
It should be pretty to close to what you're after.
If you are using Django >= 2.0, you can take advantage of a feature called autocomplete_fields. You must define search_fields on the related object’s ModelAdmin because the autocomplete search uses it.
Since you have a ForeignKey relationship to Asset in WorkOrder, in the admin.py of your app add the following:
from django.contrib import admin
#admin.register(Asset)
class AssetAdmin(admin.ModelAdmin):
search_fields = ["serial_number", "asset_tag"]
#admin.register(WorkOrder)
class WorkOrderAdmin(admin.ModelAdmin):
autocomplete_fields = ["asset"]
Add the fields you want to use for searching to search_fields, and add define autocomplete_fields as shown in the code above.
Now you can use the autocomplete_fields from django 2.0.
It's quite neat.

Show models.ManyToManyField as inline, with the same form as models.ForeignKey inline

I have a model similar to the following (simplified):
models.py
class Sample(models.Model):
name=models.CharField(max_length=200)
class Action(models.Model):
samples=models.ManyToManyField(Sample)
title=models.CharField(max_length=200)
description=models.TextField()
Now, if Action.samples would have been a ForeignKey instead of a ManyToManyField, when I display Action as a TabularInline in Sample in the Django Admin, I would get a number of rows, each containing a nice form to edit or add another Action. However; when I display the above as an inline using the following:
class ActionInline(admin.TabularInline):
model=Action.samples.through
I get a select box listing all available actions, and not a nifty form to create a new Action.
My question is really: How do I display the ManyToMany relation as an inline with a form to input information as described?
In principle it should be possible since, from the Sample's point of view, the situation is identical in both cases; Each Sample has a list of Actions regardless if the relation is a ForeignKey or a ManyToManyRelation. Also; Through the Sample admin page, I never want to choose from existing Actions, only create new or edit old ones.
I see your point but think of a case where you might need to use custom through model (table). In that case the admin inline form would include the fields for that intermediate model since thats the model you asked the admin to create the form for.
e.g.
class Person(models.Model):
name = models.CharField(max_length=128)
class Group(models.Model):
name = models.CharField(max_length=128)
members = models.ManyToManyField(Person, through='Membership')
class Membership(models.Model):
person = models.ForeignKey(Person)
group = models.ForeignKey(Group)
date_joined = models.DateField()
invite_reason = models.CharField(max_length=64)
The admin should display the form for the Memebership model cause thats the model the editable instance is related to.
In your case the through model contains only the 2 foreign keys (1 for the Action model and 1 for the Sample) ands thats why only the list of actions appear.
You could do what you are asking for if django admin supported nested inlines (there is an open ticket about that).