Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于同一页面关联数据的编辑 #39

Open
wf4867612 opened this issue May 20, 2020 · 1 comment
Open

关于同一页面关联数据的编辑 #39

wf4867612 opened this issue May 20, 2020 · 1 comment

Comments

@wf4867612
Copy link

老师,您好:
作者这个字段是不能为空的,所以PostInline 这个类,定义完之后,如果直接编辑标题和摘要就提交是会报错,owner_id 不能为空的。
我想要提交的时候直接能够把当前用户提交上去,但是在这个admin.TabularInline父类中是没有关于save方法的。

@MaoningGuan
Copy link

MaoningGuan commented Jun 12, 2020

在CategoryAdmin中添加以下函数代码:

def save_formset(self, request, form, formset, change):
    """ 修改关联对象数据 """
    instances = formset.save(commit=False)
    for instance in instances:
        if not hasattr(instance, 'owner'):  # 防止误修改其他文章的作者
            instance.owner = request.user  # 给owner字段赋值
            instance.save()
    formset.save_m2m()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants