简体   繁体   中英

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.

This order has a field created that I want to display in the admin panel but I'm getting this error:

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

You need to add readonly_fields to your OrderAdmin , and add created to that list, such as:

@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.

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