forked from DGNum/gestioCOF
AttributionForm in bda admin
- New attribution form issue less queries - Spectacle and Participant are readonly if updating an attribution. ReadOnlyMixin allows to set readonly fields only while updating an object.
This commit is contained in:
parent
bbe6f41962
commit
6451f971bd
1 changed files with 27 additions and 1 deletions
28
bda/admin.py
28
bda/admin.py
|
@ -94,6 +94,20 @@ class ParticipantAdmin(admin.ModelAdmin):
|
|||
|
||||
|
||||
class AttributionAdminForm(forms.ModelForm):
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
if 'spectacle' in self.fields:
|
||||
self.fields['spectacle'].queryset = (
|
||||
Spectacle.objects
|
||||
.select_related('location')
|
||||
)
|
||||
if 'participant' in self.fields:
|
||||
self.fields['participant'].queryset = (
|
||||
Participant.objects
|
||||
.select_related('user', 'tirage')
|
||||
)
|
||||
|
||||
def clean(self):
|
||||
cleaned_data = super(AttributionAdminForm, self).clean()
|
||||
participant = cleaned_data.get("participant")
|
||||
|
@ -106,7 +120,18 @@ class AttributionAdminForm(forms.ModelForm):
|
|||
return cleaned_data
|
||||
|
||||
|
||||
class AttributionAdmin(admin.ModelAdmin):
|
||||
class ReadOnlyMixin(object):
|
||||
readonly_fields_update = ()
|
||||
|
||||
def get_readonly_fields(self, request, obj=None):
|
||||
readonly_fields = super().get_readonly_fields(request, obj)
|
||||
if obj is None:
|
||||
return readonly_fields
|
||||
else:
|
||||
return readonly_fields + self.readonly_fields_update
|
||||
|
||||
|
||||
class AttributionAdmin(ReadOnlyMixin, admin.ModelAdmin):
|
||||
def paid(self, obj):
|
||||
return obj.participant.paid
|
||||
paid.short_description = 'A payé'
|
||||
|
@ -116,6 +141,7 @@ class AttributionAdmin(admin.ModelAdmin):
|
|||
'participant__user__first_name',
|
||||
'participant__user__last_name')
|
||||
form = AttributionAdminForm
|
||||
readonly_fields_update = ('spectacle', 'participant')
|
||||
|
||||
|
||||
class ChoixSpectacleAdmin(admin.ModelAdmin):
|
||||
|
|
Loading…
Reference in a new issue