Opened 17 months ago

Last modified 17 months ago

#34651 closed Cleanup/optimization

'list' object has no attribute '_committed' — at Initial Version

Reported by: Vinícius Febasse Owned by: nobody
Component: Uncategorized Version: 4.2
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I'm trying to work with multiple files without success. I'm working on the implementation in the admin, and then on to the form.

For a single file it works fine

from .models import ServiceOrder, MaintenanceService
from django.contrib import admin
from django.forms import Textarea

# Register your models here.
@admin.register(ServiceOrder)
class OrderServiceAdmin(admin.ModelAdmin):

list_display = ('id', 'requester', 'due_date', 'service_type', 'notes', 'status', 'registration_date')
list_display_links = ('id', 'asker',)
list_filter = ('requester', 'due_date', 'service_type', 'status', 'registration_date')
search_fields = ('requester', 'due_date', 'service_type', 'status', 'registration_date')
ordering = ('-id',)
list_per_page = 25
formfield_overrides = {

OrderService.request: {'widget': Textarea(attrs={'class': 'custom-textarea'})},

}

@admin.register(MaintenanceService)
class MaintenanceService(admin.ModelAdmin):

list_display = ('id', 'description', 'value', 'type_service', 'is_minimum_tariff', 'minimum_tariff_value', 'registration_data')
list_display_links = ('id', 'description', 'value')
list_filter = ('description', 'value', 'type_service', 'is_minimum_tariff', 'minimum_tariff_value', 'registration_data')
search_fields = ('description', 'value', 'type_service', 'is_minimum_tariff', 'minimum_tariff_value', 'registration_data')
ordering = ('description', 'value', 'type_service', 'is_minimum_rate', 'minimum_rate_value', 'registration_date')
list_per_page = 10

But when I try to do it for multiples I get this error: 'list' object has no attribute '_committed'

from .models import ServiceOrder, MaintenanceService
from django.contrib import admin
from django.forms import Textarea
from django import forms
from django.core.files import File
from django.core.files import File as DjangoFile

class MultipleFileInput(forms.ClearableFileInput):

allow_multiple_selected = True

class MultipleFileField(forms.FileField):

def init(self, *args, kwargs):

kwargs.setdefault("widget", MultipleFileInput())
super().init(*args, kwargs)

def clean(self, data, initial=None):

single_file_clean = super().clean
if isinstance(data, (list, tuple)):

result = [single_file_clean(d, initial) for d in data]

else:

result = single_file_clean(data, initial)

return result

class OrderServiceAdminForm(forms.ModelForm):

attachment = MultipleFileField(required=False)

class Meta:

model = ServiceOrder
fields="all"

# Register your models here.
@admin.register(ServiceOrder)
class OrderServiceAdmin(admin.ModelAdmin):

form = OrderServiceAdminForm

list_display = ('id', 'requester', 'due_date', 'service_type', 'notes', 'status', 'registration_date')
list_display_links = ('id', 'asker',)
list_filter = ('requester', 'due_date', 'service_type', 'status', 'registration_date')
search_fields = ('requester', 'due_date', 'service_type', 'status', 'registration_date')
ordering = ('-id',)
list_per_page = 25
formfield_overrides = {

OrderService.request: {'widget': Textarea(attrs={'class': 'custom-textarea'})},

}

def save_model(self, request, obj, form, change):

super().save_model(request, obj, form, change)

if form.cleaned_data.get('attachment'):

for file in form.cleaned_dataattached:

file_obj = DjangoFile(file, name=file.name)
obj.attachment.create(file=file_obj)

@admin.register(MaintenanceService)
class MaintenanceService(admin.ModelAdmin):

list_display = ('id', 'description', 'value', 'type_service', 'is_minimum_tariff', 'minimum_tariff_value', 'registration_data')
list_display_links = ('id', 'description', 'value')
list_filter = ('description', 'value', 'type_service', 'is_minimum_tariff', 'minimum_tariff_value', 'registration_data')
search_fields = ('description', 'value', 'type_service', 'is_minimum_tariff', 'minimum_tariff_value', 'registration_data')
ordering = ('description', 'value', 'type_service', 'is_minimum_rate', 'minimum_rate_value', 'registration_date')
list_per_page = 10

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top