简体   繁体   English

如何设置 django 管理面板来编辑 model 中的记录?

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

I have a django project created with django 4.0.我有一个用 django 4.0 创建的 django 项目。

I'm new to djnago, so if I'm asking a silly question, please forgive me!我是 djnago 的新手,所以如果我问了一个愚蠢的问题,请原谅我!

I can't figure out a way by which I can edit a record in my model through the admin panel.我想不出一种方法可以通过管理面板编辑我的 model 中的记录。

Below is the model Products that has some products added.下面是 model 添加了一些产品的产品。 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:以下是admin.py文件中的代码:

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.单击 Id 列上的数字,这些是编辑页面的超链接。 It will take you to the page where you can edit the model.它将带您到可以编辑 model 的页面。

If you want to change the link to some other field you can do that by specifying link_display_fields=['your fields']如果您想更改指向其他字段的链接,您可以通过指定 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您还可以添加 list_editable 行(例如用于产品描述)以直接在列表概述中编辑字段,如果您想跳过单击、打开然后编辑所选产品的步骤

@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:它看起来像这样: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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