如何设置Django管理面板以编辑模型中的记录?
我有一个由Django 4.0创建的Django项目。
我是Djnago的新手,所以如果我问一个愚蠢的问题,请原谅我!
我无法找到可以通过管理面板在模型中编辑记录的一种方式。
以下是产品的模型,其中添加了一些产品。 假设我想编辑字段的数据(例如产品名称),那么我该如何从 admin面板本身中实现这一目标?
管理面板仅显示 delete 当前所选记录。
下面是admin.py.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']
预先感谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
单击ID列上的数字,这些是编辑页面的超链接。它将带您进入可以编辑模型的页面。
如果要将链接更改为其他字段,则可以通过指定link_display_fields = ['your fields']来执行此操作。
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']
您还可以添加list_edistion行(例如产品说明)
要直接在列表概述中编辑字段,如果要跳过单击,打开然后编辑所选产品的步骤,
则看起来像这样:

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
It would look something like this:
