my admin.py
from django.contrib import admin
from . import models
from django.contrib.auth.models import Group, User
from .models import Product
from .forms import ForumForm
class ForumArea(admin.AdminSite):
site_header = "User Forums"
class ForumLists(admin.ModelAdmin):
list_display = ['id','sno','customer_name','date','weight','touch','percent','owner', 'product_data']
list_per_page = 20
list_filter = ['sno','customer_name','date']
search_fields = ['sno','customer_name','date']
form = ForumForm
def owner(self, obj):
subject_object = User.objects.get(id=obj.id)
return str(subject_object)
def product_data(self, obj):
subject_object = Product.objects.get(id=obj.id).name
return str(subject_object)
fieldsets = (
(("Basic Details"), {"fields": ("sno", "user", "customer_name", "date")}),
(
("Touch Details"),
{
"fields": (
"touch","percent","weight","product_type"
),
},
),
)
forum_site = ForumArea(name="ForumSite")
forum_site.register(models.forum,ForumLists)
My form.py
from django import forms
from django.forms import ModelChoiceField
from .models import *
class ForumForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
product_type = forms.ModelChoiceField(queryset=Product.objects.all())
class Meta:
fields = ('id','sno', 'user', 'customer_name', 'date', 'product_type', 'weight', 'touch', 'paisa', 'percent', 'mark')
The foreign key dropdown for product_key is showing “Product Object(1)” value I need to show it’s object value like name instead of object. I’m new to django please support.