在Django Admin中使ForeignKey可选?

Make ForeignKey optional in Django Admin?

我有一个自定义的django管理页面,我想让两个foreignkey字段在管理界面中是可选的。我不想更改基础模型。

这是模型:

1
2
3
4
5
6
7
8
9
class IncorporationTicket(models.Model, AdminURL):
    ordered_by = models.ForeignKey('Organisation', # organisation which ordered this
                                   null = True,
                                   blank = False, # i.e. can only be null as a result of delete
                                   on_delete = models.SET_NULL)
    ordered_by_individual = models.ForeignKey('Individual', # individual at organisation which ordered this
                                               null = True,
                                               blank = False, # i.e. can only be null as a result of delete
                                               on_delete = models.SET_NULL)

(adminurl是一个mixin,提供get_absolute_url)

这是ModelAdmin:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class TicketAdmin(admin.ModelAdmin):
    readonly_fields = ('ordered', 'charge', 'amount_paid', 'submitted_on')

    formfield_overrides = {
        models.ForeignKey: {'required': False},
        }


    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        pk = resolve(request.path).args[0] # the request url should only have one arg, the pk
        instance = self.get_object(request, pk)
        user = request.user
        kwargs['required'] = False # will be passed to every field

        if db_field.name =="ordered_by_individual":
            # queryset should be a union of (a) individual already set on object (b) individual for current user
            ## None option is provided by admin interface - just need to let field be optional.
            if instance.ordered_by_individual:
                kwargs["queryset"] = (
                    Individual.objects.filter(pk = instance.ordered_by_individual.pk) |
                    user.individual_set.all())
            else: kwargs["queryset"] = user.individual_set.all()
        elif db_field.name =="ordered_by":
            # queryset should be a union of (a) organisation already set (b) any organisations for which user is authorised

           try:
               individual = user.individual_set.all()[0]
               all_orgs = Organisation.all_organisations_for_which_individual_authorised_to_incorporate(individual)
           except:
               all_orgs = Organisation.objects.none()
           if instance.ordered_by:
               kwargs["queryset"] = (
                   Organisation.objects.filter(pk = instance.ordered_by.pk) |
                   all_orgs)
           else: kwargs["queryset"] = all_orgs

        return super(type(self), self).formfield_for_foreignkey(db_field, request, **kwargs)

如您所见,我试图同时使用formfield_overridesformfield_for_foreignkey在formfield上设置required = False,但没有所需的效果:当试图通过管理界面保存而不设置(即将字段保留为原始空白状态)时,管理界面显示错误"此字段是必需的"。

所以,我的问题是:如何防止底层窗体需要某些字段,同时还在formfield_for_foreignkey中设置选项?


虽然我不知道为什么kwargs['required']不起作用,但您可以用自己的表单覆盖管理表单。这并没有让我失望的神奇的姜戈管理行为,所以这是一个相当好的赌注。

1
2
3
4
5
6
7
8
9
10
class MyForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)
        self.fields['my_fk_field'].required = False

    class Meta:
         model = MyModel

class MyAdmin(admin.ModelAdmin):
    form = MyForm

这仍然允许您通过formfield_for_foo方法修改QuerySet