Home > Net >  How do i display the category name of a product instead of Category object (1) django 3.1
How do i display the category name of a product instead of Category object (1) django 3.1

Time:12-29

So my django backend is not displaying the category name but is instead giving me a category object result screenshot of undesired result under the category field. How do i fix this??

Below is a snippet of my admin.py and model.py

category/admin.py

from django.contrib import admin
from .models import Category
# Register your models here.


class CategoryAdmin(admin.ModelAdmin):
    prepopulated_fields = {'slug': ('category_name',)}
    list_display = ('category_name', 'slug')


admin.site.register(Category, CategoryAdmin)

categories/model.py


    from django.db import models

# Create your models here.


class Category(models.Model):
    class Meta:
        verbose_name = 'category'
        verbose_name_plural = 'categories'

    category_name = models.CharField(max_length=50, unique=True)
    slug = models.SlugField(max_length=100, unique=True)
    description = models.TextField(max_length=255, blank=True)
    cat_image = models.ImageField(upload_to='photos/categories', blank=True)


def __str__(self):
    return self.category_name

store/models.py


    class Product(models.Model):
    product_name = models.CharField(max_length=200, unique=True)
    slug = models.SlugField(max_length=200, unique=True)
    description = models.TextField(max_length=500, blank=True)
    price = models.IntegerField()
    images = models.ImageField(upload_to='photos/products')
    stock = models.IntegerField()
    out_of_stock = models.BooleanField(default=False)
    is_available = models.BooleanField(default=True)
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    created_date = models.DateTimeField(auto_now_add=True)
    modified_date = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.product_name

store/admin.py


    from django.contrib import admin
from .models import Product

# Register your models here.


class ProductAdmin(admin.ModelAdmin):
    list_display = ('product_name', 'price', 'category',
                    'modified_date', 'is_available', 'out_of_stock')
    prepopulated_fields = {'slug': ('product_name',)}


admin.site.register(Product, ProductAdmin)


settings.py


    INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'category',
    'accounts',
    'store',



CodePudding user response:

Check that your object's string representation aka. __str__ is part of the Category class, then the admin page should use it to show object information in that kind of list.

from django.db import models

# Create your models here.


class Category(models.Model):
    class Meta:
        verbose_name = 'category'
        verbose_name_plural = 'categories'

    category_name = models.CharField(max_length=50, unique=True)
    slug = models.SlugField(max_length=100, unique=True)
    description = models.TextField(max_length=255, blank=True)
    cat_image = models.ImageField(upload_to='photos/categories', blank=True)

    # here!
    def __str__(self):
        return self.category_name
  • Related