How to prevent self (recursive) selection for FK / MTM fields in the Django Admin

后端 未结 3 1644
余生分开走
余生分开走 2021-01-04 08:46

Given a model with ForeignKeyField (FKF) or ManyToManyField (MTMF) fields with a foreignkey to \'self\' how can I prevent self (recursive) selection within the Djan

3条回答
  •  清酒与你
    2021-01-04 09:25

    You can also override the get_form method of the ModelAdmin like so:

    def get_form(self, request, obj=None, **kwargs):
        """
        Modify the fields in the form that are self-referential by
        removing self instance from queryset
        """
        form = super().get_form(request, obj=None, **kwargs)
        # obj won't exist yet for create page
        if obj:
            # Finds fieldnames of related fields whose model is self
            rmself_fields = [f.name for f in self.model._meta.get_fields() if (
                f.concrete and f.is_relation and f.related_model is self.model)]
            for fieldname in rmself_fields:
                form.base_fields[fieldname]._queryset =
                    form.base_fields[fieldname]._queryset.exclude(id=obj.id)
        return form
    

    Note that this is a on-size-fits-all solution that automatically finds self-referencing model fields and removes self from all of them :-)

提交回复
热议问题