Reputation: 47
When I'm in admin looking at a Bar record, the dropdown for the foreign key shows the title of Foo, and not the id of Foo. There can be multiple titles with the same name, so this is not helpful. I would like to see the id of Foo while I'm looking at Bar in admin.
Here's my models.py
from django.db import models
class Foo(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
title = models.CharField(max_length=255)
class Bar(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
foo = models.ForeignKey(Foo, on_delete=models.CASCADE)
Herey's my admin.py
from django.contrib import admin
from .models import Foo, Bar
class FooAdmin(admin.ModelAdmin):
readonly_fields = ('id',)
class BarAdmin(admin.ModelAdmin):
readonly_fields = ('id',)
admin.site.register(Foo, FooAdmin)
admin.site.register(Bar, BarAdmin)
Upvotes: 0
Views: 970
Reputation: 1105
Add this in your model.
class Bar(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
foo = models.ForeignKey(Foo, on_delete=models.CASCADE)
def __str__(self):
return self.id
Upvotes: 1