Я довольно новичок в django. Я пытаюсь сохранить изменения admin.py list_editable в моей модели для следующего поля foodQuality . Я использую 'def clean (self):' в моей модели для validationError, и если нет ошибки валидации. Мне нужно сохранить свои изменения. Но это не сохранение правильного десятичного значения, которое я недавно ввел. Я попытался использовать self.cleaned_data Я получаю следующую ошибку ' объект не имеет атрибута' cleaned_data '
class DashBoard(models.Model):
"""Dashboard to be used to upload all my items"""
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
)
title = models.CharField(max_length = 255, null = True)
tranScription = models.CharField(max_length = 255, null = True)
videoUrl = models.CharField(max_length = 255, null = True)
audioUrl = models.CharField(max_length = 255, null = True)
imageUrl = models.CharField(max_length = 255, null = True)
foodQuality = models.DecimalField(max_digits=20, decimal_places=2, default=Decimal(0.00))
ambience = models.DecimalField(max_digits=20, decimal_places=2, default=Decimal(0.00))
cleanliness = models.DecimalField(max_digits=20, decimal_places=2, default=Decimal(0.00))
mediaType = models.CharField(max_length = 255, default ='mp4')
date = models.CharField(max_length = 255, default= datetime.today().strftime("%Y-%m-%d"))
isUploaded = models.BooleanField(max_length = 255, default = False)
def clean(self):
if foodQuality < 0 or foodQuality > 1:
raise ValidationError(u'%s is not between 0 and 1' % value)
def __str__(self):
return self.title
class DashBoardModel(admin.ModelAdmin):
model = models.DashBoard
actions = ['delete_model']
ordering = ['id']
list_display = ['title',
'tranScription',
'foodQuality',
'ambience',
'cleanliness',
'videoUrl',
'audioUrl',
'imageUrl',
'mediaType',
'date',
'isUploaded']
""" Following fields are made editable """
list_editable = ('foodQuality', 'ambience', 'cleanliness')
""" Below provides Link field in admin panel. Made Clickable"""
list_display_links = ('videoUrl', 'imageUrl', 'audioUrl')
""" Below provides searchFields against which search is made"""
search_fields = ('title',
'mediaType',
'tranScription',
'foodQuality',
'ambience',
'cleanliness')
fieldsets = (
(
('Permissions'),
{'fields': ('is_active', 'is_staff', 'is_superuser')}
),
(
('Important dates'), {'fields': ('last_login',)}
)
)
def delete_queryset(self, request, queryset):
queryset.delete()
def VIDEOURL(self, obj):
return format_html("<a href='{url}'>{url}</a>", url=obj.firm_url)
admin.site.register(models.User, UserAdmin)
admin.site.register(models.DashBoard, DashBoardModel)