我已经在互联网上梳理了一段时间而没有找到解决这个问题的方法。
我想做什么......
我有以下型号:
class TrackingEventType(models.Model):
required_previous_event = models.ForeignKey(TrackingEventType)
class TrackingEvent(models.Model):
tracking = models.ForeignKey(Tracking)
class Tracking(models.Model):
last_event = models.ForeignKey(TrackingEvent)
现在主要模型是跟踪,因此我的跟踪管理员看起来像这样:
class TrackingEventInline(admin.TabularInline):
model = TrackingEvent
extra = 0
class TrackingAdmin(admin.ModelAdmin):
inlines = [TrackingEventInline]
这就是当前的设置。
现在我的任务:
在TrackingAdmin中,当我添加新的TrackingEvent内联时,我想将TrackingEventType的选项限制为仅允许跟踪跟踪的最后一个TrackingEvent的那些选项。 (Tracking.last_event == TrackingEventType.required_previous_event)。
为此,我需要能够访问InlineTrackingEvent上的相关跟踪,以访问last_event并相应地过滤TrackingEventType的选项。
所以我发现了这个:Accessing parent model instance from modelform of admin inline,但是当我相应地设置TrackingEventInline时:
class MyFormSet(forms.BaseInlineFormSet):
def _construct_form(self, i, **kwargs):
kwargs['parent_object'] = self.instance
print self.instance
return super(MyFormSet, self)._construct_form(i, **kwargs)
class MyForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
print kwargs
self.parent_object = kwargs.pop('parent_object')
super(MyForm, self).__init__(*args, **kwargs)
class TrackingEventInline(admin.TabularInline):
form = MyForm
formset = MyFormSet
model = TrackingEvent
extra = 0
我从KeyError at /admin/.../tracking/2/change/ 'parent_object'
self.parent_object = kwargs.pop('parent_object')
有谁知道如何解决这个问题?我是以错误的方式解决问题的吗?我想在前端的自定义表单中这很容易,但我真的想使用管理员,因为整个应用程序都是为了从管理员那里使用而构建自定义的,这将是一项很大的工作。管理界面只是因为这个问题:)
答案 0 :(得分:0)
好的,所以在StackOverflow上发帖总是有助于解决问题。我能够整理出适合我的解决方案。
它包括在外部函数中定义我自己的Form,以及为TrackingEvent定义两个InlineAdmin对象(一个用于更新/编辑,一个用于插入)。
以下是代码:
def create_trackingevent_form(tracking):
"""
"""
class TrackingEventForm(forms.ModelForm):
"""
Form for Tracking Event Inline
"""
def clean(self):
"""
May not be needed anymore, since event type choices are limited when creating new event.
"""
next_eventtype = self.cleaned_data['event_type']
tracking = self.cleaned_data['tracking']
# get last event, this also ensures last_event gets updated everytime the change form for TrackingEvent is loaded
last_eventtype = tracking.set_last_event()
if last_eventtype:
last_eventtype = last_eventtype.event_type
pk = self.instance.pk
insert = pk == None
# check if the event is updated or newly created
if insert:
if next_eventtype.required_previous_event == last_eventtype:
pass
else:
raise forms.ValidationError('"{}" requires "{}" as last event, "{}" found. Possible next events: {}'.format(
next_eventtype,
next_eventtype.required_previous_event,
last_eventtype,
'"%s" ' % ', '.join(map(str, [x.name for x in tracking.next_tracking_eventtype_options()]))
)
)
else:
pass
return self.cleaned_data
def __init__(self, *args, **kwargs):
# You can use the outer function's 'tracking' here
self.parent_object = tracking
super(TrackingEventForm, self).__init__(*args, **kwargs)
self.fields['event_type'].queryset = tracking.next_tracking_eventtype_options()
#self.fields['event_type'].limit_choices_to = tracking.next_tracking_eventtype_options()
return TrackingEventForm
class TrackingEventInline(admin.TabularInline):
#form = MyForm
#formset = MyFormSet
model = TrackingEvent
extra = 0
#readonly_fields = ['datetime', 'event_type', 'note']
def has_add_permission(self, request):
return False
class AddTrackingEventInline(admin.TabularInline):
model = TrackingEvent
extra = 0
def has_change_permission(self, request, obj=None):
return False
def queryset(self, request):
return super(AddTrackingEventInline, self).queryset(request).none()
def get_formset(self, request, obj=None, **kwargs):
if obj:
self.form = create_trackingevent_form(obj)
return super(AddTrackingEventInline, self).get_formset(request, obj, **kwargs)
我希望这可以帮助其他人遇到同样的问题。有些人认为Stack Overflow线程帮助我想出了这个:
Prepopulating inlines based on the parent model in the Django Admin
Limit foreign key choices in select in an inline form in admin
https://docs.djangoproject.com/en/1.9/ref/models/instances/#django.db.models.Model.clean_fields
如果您有任何问题,请随时提出问题