我有两个模型,一个MainModel和一个相关的InlineModel,我想在管理员中显示为内联。此InlineModel可用于,例如,记录模型,并应跟踪登录的管理员用户进行更改。虽然这看起来很简单(实际上,当用户字段是MainModel的一部分时,文档显示了这个例子),当字段在内联时,我似乎无法掌握它。
具体来说,我的目标是:
我的问题:
以下是我目前的想法:
#models.py
class MainModel(models.Model):
some_info = models.IntegerField()
class InlineModel(models.Model):
main = models.ForeignKey(MainModel)
data = models.CharField(max_length=255)
user = models.ForeignKey('auth.User')
#admin.py
class InlineModelInline(admin.TabularInline):
model = InlineModel
fields = ('data', 'user')
#readonly_fields = ('data', 'user') #Bonus question later
class MainModelAdmin(admin.ModelAdmin):
list_display = ('id', 'some_info')
inlines = [InlineModelInline]
#def save_model(self, request, obj, form, change):
#http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_model
#Only called for MainModel, not for any of the inlines
#Otherwise, would be ideal
def save_formset(self, request, form, formset, change):
#http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_formset
#Experimenting showd this is called once per formset (where the formset is a group of inlines)
#See code block at http://code.djangoproject.com/browser/django/tags/releases/1.2.1/django/contrib/admin/options.py#L894
if not isinstance(formset.model, InlineModel):
return super(MainModelAdmin, self).save_formset(request, form, formset, change)
instances = formset.save(commit=False)
for instance in instances:
if not instance.pk:
instance.user = request.user
instance.save()
formset.save_m2m()
答案 0 :(得分:9)
我已经解决了问题的前半部分:
def save_formset(self, request, form, formset, change):
if formset.model != InlineModel:
return super(MainModelAdmin, self).save_formset(request, form, formset, change)
instances = formset.save(commit=False)
for instance in instances:
if not instance.pk:
instance.user = request.user
instance.save()
formset.save_m2m()
现在我对奖金行为感兴趣:
由于验证规则,我需要在添加新内联时选择用户。我最好的猜测是不在我的InlineModelInline.fields元组中包含'user'字段,但是这不会向作者显示现有的InlineModel实例。 (编辑:将'user'添加到readonly_fields在这里工作)
(编辑)如何使现有的内联呈现'数据'为只读,但在添加新的内联时仍能编辑它?
答案 1 :(得分:1)
它对我有用。 This方法不允许我删除内联项。
def save_formset(self, request, form, formset, change):
for form in formset.forms:
form.instance.user = request.user
formset.save()
答案 2 :(得分:0)
回答奖励问题:“如何将现有内联呈现'数据'作为只读,但在添加新内联时仍能编辑它?”:
我对同一个模型使用两个内联:
#admin.py
class InlineModelInline(admin.TabularInline):
model = InlineModel
extra = 1
max_num = 1
#admin.py
class InlineModelExistingInline(admin.TabularInline):
model = InlineModel
readonly_fields = ('data', 'user') #All Fields here except pk
can_delete = False
extra = 0
max_num = 0
class MainModelAdmin(admin.ModelAdmin):
...
inlines = [InlineModelInline, InlineModelExistingInline]
...