简体   繁体   English

Django:无法为订单模型表单指定“已创建”,因为它是不可编辑的字段

[英]Django: 'created' cannot be specified for Order model form as it is a non-editable field

I've a model called Order, that stores information about the order placed by each user.我有一个名为 Order 的模型,它存储有关每个用户所下订单的信息。

This order has a field created that I want to display in the admin panel but I'm getting this error:此订单created了一个我想在admin panel中显示的字段,但我收到此错误:

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模型.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管理员.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

You need to add readonly_fields to your OrderAdmin , and add created to that list, such as:您需要将readonly_fields添加到您的OrderAdmin中,并将created添加到该列表中,例如:

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

The Django docs describe that when using fieldsets which contain read-only fields, those fields must be listed in readonly_fields for them to be displayed. Django 文档描述了当使用fieldsets只读字段的字段集时,这些字段必须列在readonly_fields中才能显示。

暂无
暂无

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

相关问题 无法为文章模型表单指定“内容”,因为它是不可编辑的字段 - 'content' cannot be specified for Article model form as it is a non-editable field django.core.exceptions.FieldError:不能为论坛 model 表单指定“日期”,因为它是不可编辑的字段 - django.core.exceptions.FieldError: 'date' cannot be specified for Forum model form as it is a non-editable field 如何在 Django 中向自定义管理表单添加不可编辑的字段 - How do you add a non-editable field to a custom admin form in Django 创建一个仅显示(不可编辑)Django 管理字段 - Creating a display-only (non-editable) Django admin field (Django REST)覆盖(视图)`create` 方法中的不可编辑字段 - (Django REST) Override Non-Editable Field in the (Views) `create` Method 如何在模型类的Flask Admin视图中使字段不可编辑 - How to make a field non-editable in Flask Admin view of a model class Django管理员,使用DateTime小部件在创建时设置了不可编辑的日期字段 - Django admin, setting a non-editable date field on creation with the DateTime widget 如何在 django 管理员中创建“仅创建”不可编辑字段 - How to make a “create-only” non-editable field in django admin Django:如何在内联模型表单集中默认情况下使字段不可编辑? - Django: How do I make fields non-editable by default in an inline model formset? 输入字段中是否有不可编辑前缀的配置选项 - Is there a config option for non-editable prefixes in entry field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM