admin.py 1009 B

1234567891011121314151617181920212223242526272829303132333435
  1. from django import forms
  2. from django.contrib import admin
  3. from django.core.exceptions import ValidationError
  4. from import_export.admin import ImportExportModelAdmin
  5. from .models import Team
  6. from .resources import TeamResource
  7. class TeamForm(forms.ModelForm):
  8. class Meta:
  9. model = Team
  10. fields = "__all__"
  11. def clean(self):
  12. cleaned_data = super().clean()
  13. organization = cleaned_data.get("organization")
  14. projects = cleaned_data.get("projects")
  15. if projects.exclude(organization=organization).exists():
  16. raise ValidationError(
  17. "All projects must belong to the same organization as the team."
  18. )
  19. return cleaned_data
  20. class TeamAdmin(ImportExportModelAdmin):
  21. form = TeamForm
  22. search_fields = ("slug",)
  23. list_display = ("slug", "organization")
  24. raw_id_fields = ("organization",)
  25. filter_horizontal = ("members", "projects")
  26. resource_class = TeamResource
  27. admin.site.register(Team, TeamAdmin)