Home > OS >  How to print an object of a foreignkey instead of the entire class to Django administration
How to print an object of a foreignkey instead of the entire class to Django administration

Time:10-03

In my 'models.py' file, I have 2 LOOKUP tables: the 'Part' class and the 'Vendor' class as shown:

# Part Lookup table
class Part(models.Model):
    part_id = models.CharField(max_length=64)                               # Part ID
    description = models.CharField(max_length=64)                           # Part description
    pq = models.DecimalField(max_digits=7, decimal_places=2)                # Pack quantity
    mrrp = models.DecimalField(max_digits=10, decimal_places=2)             # Manufacturers Recommended Retail Price                                         
    # Display something in admin
    def __str__(self):
        return f"{self.id} {self.part_id} {self.description} {self.pq} {self.mrrp}"   

# Vendor Lookup table
class Vendor(models.Model):
    name = models.CharField(max_length=64)                         
    # Display something in admin
    def __str__(self):
        return f"{self.id} {self.name}"  

I then have another FACT or ASSOCIATION table, the 'Relationship' class as shown:

# Relationship association table (Between Part & Vendor)
class Relationship(models.Model):
    part_id = models.ForeignKey(Part, on_delete=models.CASCADE, related_name="part")  
    vendor_id = models.ForeignKey(Vendor, on_delete=models.CASCADE, related_name="vendor")  
    # Display something in admin
    def __str__(self):
        return f"{self.id} {self.part_id} {self.vendor_id}"  

The below is my entire 'admin.py' file:

from django.contrib import admin

# Import the classes from models.py
from .models import Part, Vendor, Relationship, Transaction, Offer, Supersession, Category, Group

# Django admin title
admin.site.site_header = "Online Parts System"

# Model Admin class
class PartAdmin(admin.ModelAdmin):
    # Data headers (must match objects of the Part class)
    list_display = ('id', 'part_id', 'description', 'pq', 'mrrp')

# Model Admin class
class VendorAdmin(admin.ModelAdmin):
    # Data headers (must match objects of the Vendor class)
    list_display = ('id', 'name')

# Model Admin class
class RelationshipAdmin(admin.ModelAdmin):
    # Data headers (must match objects of the Vendor class)
    list_display = ('id', 'part_id', 'vendor_id')

# Register your models here.
admin.site.register(Part, PartAdmin)
admin.site.register(Vendor, VendorAdmin)
admin.site.register(Relationship, RelationshipAdmin)

Everything is working really well so far. Unfortunately, in the Django admin page > Relationships table, under the 'Part ID' column, I am seeing every object of the 'Part' class (since I used the ForeignKey line).

I only wish to see one object such as 'part_id' instead of all the others combined.

What is the simplest way to achieve this?

Please see image below. I basically want to simplify the string underlined in red.

Django Admin Page So Far

CodePudding user response:

The string used in the admin dashboard under the part_id column comes from the __str__ method of the Part model. You can change the implementation of the __str__ method to include only the part_id.

# Part Lookup table
class Part(models.Model):
    part_id = models.CharField(max_length=64)                               # Part ID
    description = models.CharField(max_length=64)                           # Part description
    pq = models.DecimalField(max_digits=7, decimal_places=2)                # Pack quantity
    mrrp = models.DecimalField(max_digits=10, decimal_places=2)             # Manufacturers Recommended Retail Price                                         
    # Display something in admin
    def __str__(self):
        return f"{self.part_id}"
  • Related