简体   繁体   中英

How to setup django admin panel to edit a record in a model?

I have a django project created with django 4.0.

I'm new to djnago, so if I'm asking a silly question, please forgive me!

I can't figure out a way by which I can edit a record in my model through the admin panel.

Below is the model Products that has some products added. Suppose I want to edit the fields' data (eg. Name of product), then how can I achieve that from the admin panel itself?

The admin panel is just showing to delete the selected record currently.

型号页面

选择后的选项

Below is the code that is present in the admin.py file:

from django.contrib import admin

from .models import (
    Customer, Product, Cart, OrderPlaced
)


@admin.register(Customer)
class CustomerModelAdmin(admin.ModelAdmin):
    list_display = ['id', 'user', 'name', 'locality', 'city', 'zipcode',
                    'state']


@admin.register(Product)
class ProductModelAdmin(admin.ModelAdmin):
    list_display = ['id', 'title', 'selling_price', 'discounted_price',
                    'description', 'brand', 'category', 'product_image']


@admin.register(Cart)
class CartModelAdmin(admin.ModelAdmin):
    list_display = ['id', 'user', 'product', 'quantity']


@admin.register(OrderPlaced)
class OrderPlacedModelAdmin(admin.ModelAdmin):
    list_display = ['id', 'user', 'customer', 'product', 'quantity',
                    'ordered_date', 'status']

Thanks in advance!

Click on the numbers on the Id column, those are the hyperlinks for the edit page. It will take you to the page where you can edit the model.

If you want to change the link to some other field you can do that by specifying link_display_fields=['your fields']

You can also add the list_editable line (eg. for product description) to edit the field directly in the list overview, if you want to skip the step of clicking, opening and then editing the selected product

@admin.register(Product)
class ProductModelAdmin(admin.ModelAdmin):
    list_display = ['id', 'title', 'selling_price', 'discounted_price',
                    'description', 'brand', 'category', 'product_image']

    list_editable = ('description',)

It would look something like this: 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM