ManyToMany Field on Django Admin Interface - django

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.

Related

Hide Model Property from Django admin and set Its Value Manually

I want to hide a column from django admin and set its value from session.
In simple words i want to set a property of a model from session and don't want a field in admin for that column.
Can someone help me?
You can mark the field as read-only but still have it visible in the admin, or completely exclude it.
class MyModel(models.Model):
field1 = models.CharField(max_length=20) # this is editable
field2 = models.CharField(max_length=20, editable=False) # this is not
or
#admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
exclude = ['field2']
If you're using a custom model form in the admin, just don't include the field in the fields attribute.
As far as I remember there are a lot of ways to do this, with the fields attribute on Meta you can hide or select which fields to show.
check This example

Showing more than '__str__' for a Django foreign key field

I've two simple Django model classes,
models.py
from django.db import models
class ParentModel(models.Model):
small_text = models.CharField(max_length=20)
big_text = models.CharField(max_length=500)
def __str__(self):
return self.small_text
class ChildModel(models.Model):
parent = models.ForeignKey(ParentModel)
def __str__(self):
return '%s is my parent' % self.parent
admin.py
from django.contrib import admin
import models
admin.site.register(models.ChildModel)
admin.site.register(models.ParentModel)
So the default view is you see the 'small_text' in a select element in the admin section. What I'd love to be able to do is extend that so that there's another TextArea, or something else I can , underneath the select which changes as you choose a different Daddy.
I've looked into a few different ways to do this, but they all seem hella complicated for what with Django, I'd have thought should be an easy task. Any ideas?
If you're looking to be able to change ChildModel's properties while viewing the ParentModel in the admin, you should look into using an inline in the admin
If you're looking to have additional fields appear when viewing the index page in the admin for a model, then you'll want to add additional properties to the list_display property on the model's admin class.

Accessing a model variable in Django

Obviously I am new to Django, because I would assume this is relatively simple.
Lets say in my models.py I created a model "User", with two fields, a "username" and a "email" field. In a form called "UserForm", I want to access a list of all the "username"s in the "User" model. This list would then be used to populate a dropdown menu using Select.
I feel like this should be really easy, and I have been looking for some simple way to do it. I can find lots of ways that aren't all inclusive (ie filter(username = "Joe")), but I can't find one that will list all the users.
Any help would be greatly appreciated.
You're looking for a ModelChoiceField. Its queryset property can be populated from an ORM call, getting you all of the Users. Have a look at the section Creating Forms from Models in the docs for more information.
from django.contrib.auth.models import User
class UserForm(forms.ModelForm):
class Meta:
model = # Your model here
def __init__(self, *args, **kwargs):
self.fields['user'] = forms.ModelChoiceField(queryset=User.objects.all())
ForeignKey fields will be automatically shown as ModelChoiceFields, but you can always override the choices if you need.

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