Django - one-to-one modelAdmin

回眸只為那壹抹淺笑 提交于 2019-12-06 07:58:51

You can create a custom form in your admin and change the queryset value of the resource field. Something like this:

admin.py

from django import forms
from django.db.models import Q

from .models import Intake

class IntakeForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(IntakeForm, self).__init__(*args, **kwargs)
        self.fields['resource'].queryset = Resource.objects.filter(
            Q(intake__isnull=True) | Q(intake=self.instance)
        )

class IntakeAdmin(admin.ModelAdmin):
    form = IntakeForm

admin.site.register(Intake, IntakeAdmin)

You could probably use limit_choices_to on the field definition:

resource = models.OneToOneField(Resource, verbose_name="Allocation",
                                limit_choices_to={'intake__isnull': True})
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!