Django InlineModelAdmin - 在保存时根据请求设置内联字段(自动设置用户字段)(save_formset 与 save_model)

发布于 2024-09-05 21:08:27 字数 2129 浏览 7 评论 0原文

我有两个模型,一个 MainModel 和一个相关的 InlineModel,我想在管理中将其显示为内联模型。例如,此 InlineModel 可用于记录模型,并应跟踪登录的管理员用户所做的更改。虽然这看起来很简单(事实上,当用户字段是 MainModel 的一部分时,文档显示了一个示例),但当该字段位于 Inline 上时,我似乎无法理解它。

具体来说,我的目标是:

  1. 用户编辑 MainModel
  2. 用户添加 InlineModel,而不填写用户字段
  3. 用户按下保存
  4. 代码填充新创建的 InlineModel 实例的用户字段
  5. (奖励!用户字段对于现有实例是只读的,对于新实例是隐藏的)内联)

我的问题是:

  1. 这是正确的吗? InlineModelAdmin 实例也不会调用它的 save_model
  2. 这样做是否允许我保存而不会导致错误? (用户是必需的,验证标记它)
  3. 如何隐藏新内联的用户输入字段,并使其对于现有内联只读?

以下是我目前的想法:


#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()

I have two models, a MainModel and a related InlineModel that i'd like to show as an inline in the admin. This InlineModel can be used for, say, making notes about the model and should track the logged in admin user making changes. While this seems simple (and indeed, the docs show an example for this when the user field is part of the MainModel), I can't seem to grasp it when the field is on the Inline.

To be specific, my goal is:

  1. User edits MainModel
  2. User adds an InlineModel, not filling in the user field
  3. User presses save
  4. Code fills in the user field for newly created InlineModel instances
  5. (Bonus! user field is readonly for existing instances and hidden for new inlines)

And my questions:

  1. Is this correct? Its too bas save_model isn't called for InlineModelAdmin instances
  2. Does doing it this way allow me to save without causing an error? (user is required, validation flags it)
  3. How can I hide the user input field for new inlines, and have it readonly for existing inlines?

Here are my current ideas:


#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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

早茶月光 2024-09-12 21:08:27

我已经解决了问题的前半部分:

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()

现在我对奖励行为感兴趣:

  1. 由于验证规则,我需要在添加新内联时选择用户。我最好的猜测是不在我的 InlineModelInline.fields 元组中包含“用户”字段,但这不会显示现有 InlineModel 实例的作者。 (编辑:将“用户”添加到 readonly_fields 在这里有效)

  2. (编辑)如何使现有内联将“数据”呈现为只读,但在添加新内联时仍然能够对其进行编辑?

I have solved the first half of my question:

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()

Now i'm interested in the bonus behavior:

  1. I'm required to select a user when adding a new inline due to validation rules. My best guess is to not include the 'user' field in my InlineModelInline.fields tuple, but then this won't show the author for existing InlineModel instances. (Edit: adding 'user' to readonly_fields works here)

  2. (Edit) How can I make the existing inlines render 'data' as readonly, but still be able to edit it when adding a new inline?

嘿看小鸭子会跑 2024-09-12 21:08:27

这对我有用。 此方法不允许我删除内联项目。

def save_formset(self, request, form, formset, change):
    for form in formset.forms:
        form.instance.user = request.user
    formset.save()

It worked for me. This approach won't allow me to delete Inline items.

def save_formset(self, request, form, formset, change):
    for form in formset.forms:
        form.instance.user = request.user
    formset.save()
杯别 2024-09-12 21:08:27

要回答奖励问题:“如何使现有内联将‘数据’呈现为只读,但在添加新内联时仍然能够编辑它?”:

我对同一模型使用两个内联:

#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]
    ...

To answer the Bonus Question: "How can I make the existing inlines render 'data' as readonly, but still be able to edit it when adding a new inline?":

I use two inlines for the same model:

#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]
    ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文