简体   繁体   English

如何仅使用当前用户的authors(fk)来修改通用CreateView中的作者选择字段?

[英]How to modify the author choices field in my generic CreateView with authors(fk) only from the current user?

This is my code, i have read the documentations and it seems this method is the right way, i get no errors but i see no results. 这是我的代码,我已经阅读了文档,看来此方法是正确的方法,我没有收到任何错误,但没有看到任何结果。 Can somebody help me in what i am doing wrong? 有人可以帮我解决我做错的事情吗?

class BookCreate(LoginRequiredMixin, CreateView):
     model = Book
     fields = ['title', 'isbn', 'year', 'author', 'publisher']


    def form_valid(self, form):
        form.instance.owner = self.request.user
        return super(BookCreate, self).form_valid(form)

    def form_valid(self, form):
        b = Book.objects.all
        form.instance.author = ModelChoiceField(queryset=b.author_set.filter(owner=self.request.user))
        return super(BookCreate, self).form_valid(form)

It is much easier to simply exclude the author from the list of fields, then set it in the form_valid method: 只需将author从字段列表中排除,然后在form_valid方法中进行设置,就form_valid

class BookCreate(LoginRequiredMixin, CreateView):
    model = Book
    fields = ['title', 'isbn', 'year', 'publisher']

    def form_valid(self, form):
        form.instance.owner = self.request.user
        return super(BookCreate, self).form_valid(form)

If you do this, make sure you delete your second form_valid method, which is replacing the correct form_valid method above. 如果这样做,请确保删除第二个form_valid方法,该方法将替换上面的正确form_valid方法。

If you must include author as a field with a single option, then the code is much more complicated. 如果必须使用单个选项将author作为字段包括在内,则代码要复杂得多。 You need a custom form with an __init__ method which takes user and sets the queryset for the auth field. 您需要一个带有__init__方法的自定义表单,该方法接受user并设置auth字段的queryset。

Then you need to modify your view to use your custom form, and override get_form_kwargs to include self.request.user . 然后,您需要修改视图以使用自定义表单,并覆盖get_form_kwargs以包括self.request.user

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django — 如何从一组“作者”(用户)中过滤具有“作者”的对象? - Django — How to filter objects with an “author” from a set of “authors”(users)? 如何在Django中使用CreateView / UpdateView在表单中使用当前用户填写字段? - How can I fill a field with current user in form using CreateView/UpdateView in Django? 限制CreateView中的表单字段不将当前用户显示为模板中的选择 - Restrict form field in CreateView to not show current-user as a choice in template Django:如何使用通用CreateView在注册后直接登录用户 - Django: How to login user directly after registration using generic CreateView 如何用当前用户名填写作者字段 - how to fill an author field with current username 如何从 CreateView 将用户链接到模型? - how to link User to Model from CreateView? 如何在 Model 序列化程序中向当前用户声明作者? - How to declare author to current user in Model serializer? 如何在通用Django CreateView中包含来自另一个模型的对象? - How to include an object from another model in a generic Django CreateView? 为字段添加自定义验证,用于通用视图CreateView - Adding custom validation to a field, for the generic view CreateView 使用createview和modelform在django中自动将登录用户设置为作者 - Automatically set logged-in user as the author in django using createview and modelform
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM