How to use the same ModelForm form different admin classes with different models

I'm trying to create a common form with a date range validation that will be used in 20 admin classes with different model

So I'm creating this mixin for each of them to use

class DateControllerMixin():
    def get_queryset(self, request):
        qs = dateControlQuerySet(super().get_queryset(request), self.parameter_name, request)
        return qs

    

    def get_form(self, request, obj=None, change= None,**kwargs):
        print(self.parameter_name)
        if request.user.groups.filter(name='slaughterhouse_date_controlled').exists():
            form = DateControlledForm
            form.model_class = self.model
            form.parameter_name = self.parameter_name
            return form
        return super().get_form(request, obj, change, **kwargs)


And this is the form but I can't find a way to make the form without specifying the model class in the Meta or use class attributes

class DateControlledForm(forms.ModelForm):
    def __init__(
        self, *args, **kwargs
    ):
        self._meta.model = self.model_class
        super(DateControlledForm, self).__init__(*args, **kwargs)
    class Meta:
        # model = self.model_class
        fields='__all__'
        widgets = {
            'the_date': AdminDateWidget(),
        }

    def clean(self, **kwargs):
        print(self.paramter_name)
        date = self.cleaned_data.get('the_date')
        today = datetime.today().date()
        days_limit = AppConfigurations.objects.get(parameter_name=self.parameter_name).parameter_value
        first_day = today - timedelta(days = int(days_limit))
        
        if date < first_day:
            raise forms.ValidationError({
                'the_date': [_(f"Date cannot be before {first_day}.")]})
        
        return self.cleaned_data

I tried to edit the meta options from the init method but it didn't work

Back to Top