gpt4 book ai didi

python - Django:无法为订单模型表单指定 'created',因为它是不可编辑的字段

转载 作者:行者123 更新时间:2023-12-01 01:19:25 25 4
gpt4 key购买 nike

我有一个名为 Order 的模型,用于存储有关每个用户所下订单的信息。

此订单有一个已创建字段,我想在管理面板中显示该字段,但我收到此错误:

FieldError at /admin/order/order/1/change/

'created' cannot be specified for Order model form as it is a non-editable field. Check fields/fieldsets/exclude attributes of class OrderAdmin.

models.py

class Order(models.Model):
token = models.CharField(max_length=100, blank=True, null=True)
first_name = models.CharField(max_length=50, blank=True, null=True)
last_name = models.CharField(max_length=50, blank=True, null=True)
total = models.DecimalField(max_digits=10, decimal_places=2)
email = models.EmailField(max_length=250, blank = True, verbose_name= 'Correo electrónico')
last_four = models.CharField(max_length=100, blank=True, null=True)
created = models.DateTimeField(auto_now_add=True)
shipping_address1 = models.CharField(max_length=100, blank=True, null=True)
shipping_address2 = models.CharField(max_length=100, blank=True, null=True)
shipping_department = models.CharField(max_length=100, blank=True, null=True)
shipping_province = models.CharField(max_length=100, blank=True, null=True)
shipping_district = models.CharField(max_length=100, blank=True, null=True)
reason = models.CharField(max_length=400, blank=True, null=True, default='')


class Meta:
db_table = 'Order'
ordering = ['-created']

def __str__(self):
return str(self.id)




class OrderItem(models.Model):
order = models.ForeignKey(Order, on_delete=models.CASCADE)
product = models.CharField(max_length= 200)
quantity = models.CharField(max_length= 200)
size = models.CharField(max_length=200)
price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name= 'PEN Price')
image = models.ImageField(upload_to='images', blank=True, null=True)
comment = models.CharField(max_length=200, blank=True, null=True, default='')
uploaded_at = models.DateTimeField(auto_now_add=True)



class Meta:
db_table = "OrderItem"

def sub_total(self):
return self.quantity * self.price


@property
def image_filename(self):
return self.image.url.split('/')[-1]


def __str__(self):
return self.product

admin.py

from django.contrib import admin
from .models import Order, OrderItem


class OrderItemAdmin(admin.TabularInline):
model = OrderItem
fieldsets = [
# ('Customer', {'fields': ['first_name', 'last_name'], }),
('Product', {'fields': ['product'],}),
('Quantity', {'fields': ['quantity'],}),
('Price', {'fields': ['price'], }),
]
readonly_fields = ['product', 'quantity', 'price']
can_delete = False
max_num = 0
template = 'admin/order/tabular.html'

### Order Display ###



@admin.register(Order)
class OrderAdmin(admin.ModelAdmin):
model = Order
list_display = ['token', 'first_name', 'last_name', 'email', 'total', 'reason', 'created']
list_editable = ['reason',]
list_display_links = ('token', 'email')
search_fields = ['token', 'shipping_department', 'email']
fieldsets = [
('ORDER INFORMATION', {'fields': ['token', 'total', 'created']}),
('SHIPPING INFORMATION', {'fields': ['first_name', 'last_name', 'shipping_department', 'shipping_province',
'shipping_district']}),
]

inlines = [
OrderItemAdmin,
]

def has_delete_permission(self, request, obj=None):
return False

def has_add_permission(self, request):
return False

最佳答案

您需要将 readonly_fields 添加到您的 OrderAdmin 中,并将 created 添加到该列表中,例如:

@admin.register(Order)
class OrderAdmin(admin.ModelAdmin):
...
readonly_fields = ['created']

The Django docs describe当使用包含只读字段的 fieldsets 时,这些字段必须列在 readonly_fields 中才能显示。

关于python - Django:无法为订单模型表单指定 'created',因为它是不可编辑的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53989455/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com