How to Change Foreignkey Display Text in the Django Admin

How to change ForeignKey display text in the Django Admin?

If you want it to take effect only in admin, and not globally, then you could create a custom ModelChoiceField subclass, use that in a custom ModelForm and then set the relevant admin class to use your customized form.
Using an example which has a ForeignKey to the Person model used by @Enrique:

class Invoice(models.Model):
person = models.ForeignKey(Person)
....

class InvoiceAdmin(admin.ModelAdmin):
form = MyInvoiceAdminForm

class MyInvoiceAdminForm(forms.ModelForm):
person = CustomModelChoiceField(queryset=Person.objects.all())
class Meta:
model = Invoice

class CustomModelChoiceField(forms.ModelChoiceField):
def label_from_instance(self, obj):
return "%s %s" % (obj.first_name, obj.last_name)

How to show foreignkey attributes django admin fields?

If I understood you correctly, This should work.

class Classes(models.Model):
classes_title = models.CharField(max_length=200)
classes_content = models.TextField()
classes_category = models.ForeignKey(Category, on_delete=models.SET_DEFAULT)

def __str__(self):
return self.title

class Subjects(models.Model):
subject_title = models.CharField(max_length=200)
subject_content = models.TextField()
subject_class = models.ForeignKey(Classes, on_delete=models.SET_DEFAULT)

def __str__(self):
return f"{self.subject_title} - {str(self.subject_class)}"

Django Admin display foreign key value

you have to tell django what to show when representing object category as string. for example

class Category(models.Model):
category_name = models.CharField(max_length=50)

def __str__(self):
return self.category_name

Displaying ForeignKey data in Django admin change/add page

If you don't need to edit it, you can display it as a readonly field:

class DownloadAdmin(admin.ModelAdmin):
readonly_fields = ('task_added_at',)

def task_added_at(self, obj):
return obj.task.added_at

How to change User representation in Django Admin when used as Foreign Key?

I think __unicode__() method is not the correct, you should use __str__() method.

For Python 2.x, __str__() method will return str(bytes) and __unicode__() method will return unicode (text).

The print statement and the str built-in call __str__() to determine
the human-readable representation of an object. The unicode built-in
calls __unicode__() if it exists, and otherwise falls back to
__str__() and decodes the result with the system encoding. Conversely, the Model base class automatically derives __str__() from
__unicode__() by encoding to UTF-8.
read here complete

But in Python 3.x there is just __str__(), no __unicode__() method.

Django provides a simple way to define __str__() and __unicode__()
methods that work on Python 2 and 3: you must define a __str__()
method returning text and to apply the python_2_unicode_compatible()
decorator.

On Python 3, the decorator is a no-op. On Python 2, it defines
appropriate __unicode__() and __str__() methods (replacing the
original __str__() method in the process).

Here is an example from django docs.

from django.utils.encoding import python_2_unicode_compatible

@python_2_unicode_compatible
class MyClass(object):
def __str__(self):
return "Instance of my class"

SOLUTION : Decorate in the same way, as done above for your Class and
in models.py, add a method which will be get added to the User model.

from django.contrib.auth.models import User

def get_name(self):
return '{} {}'.format(self.first_name, self.last_name)

User.add_to_class("__str__", get_name)


Related Topics



Leave a reply



Submit