English 中文(简体)
不包括形式领域,但又加上清洁
原标题:Excluding a form field, yet adding it back in with clean()

在django admin,我想自动填满观察用户。 在<代码>clean功能期间,填入created_by field with request.user。 问题在于,自<代码>created_by以来 表格排除了外地,在<代码>上插入的价值显然被忽视。 我如何能够这样做? 我想根本就不展示植被。

class NoteInline(admin.TabularInline):
    model = Note
    extra = 1
    can_delete = False

    def get_formset(self, request, obj=None, **kwargs):
        """
        Generate a form with the viewing CSA filled in automatically
        """

        class NoteForm(forms.ModelForm):

            def clean(self):
                self.cleaned_data[ created_by ] = request.user
                return self.cleaned_data

            class Meta:
                exclude = ( created_by , )
                model = Note
                widgets = { note : forms.TextInput(attrs={ style : "width:80%"})}

        return forms.models.inlineformset_factory(UserProfile, Note,
                                                  extra=self.extra,
                                                  form=NoteForm,
                                                  can_delete=self.can_delete)
问题回答

政府专家小组:

为什么不仅离开了现场,而不是排除它,然后使之成为隐蔽的东西?

def __init__(*args, **kwargs):
   super(NoteForm, self).__init__(*args, **kwargs)

   self.fields[ created_by ].widget = forms.widgets.HiddenInput()


#rest of your form code follows, except you don t exclude  created_by  any more    

政府专家小组第2号(因为隐蔽领域仍然出现在网上的栏目中):

Don t 自行决定。 清洗——完全采用清洁(清洗)方法生成的数据。 相反,它压倒了“灯光”并把它放在那里。

(在请求中,如果你能够,或通过添加到<条码>上>,或将其用作似乎已存在的一类变量,则请予以撤销。)

我的解决办法是将“<代码>formfield_for_for_foreignkey功能用于“Inline”,该功能将降幅限制在用户中。

class NoteInline(admin.TabularInline):
    model = Note
    extra = 1
    can_delete = False

    def queryset(self, request):
        return Note.objects.get_empty_query_set()

    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name ==  created_by :
            # limit the  created_by  dropdown box to just the CSR user who is
            # logged in and viewing the page.
            kwargs[ queryset ] = User.objects.filter(pk=request.user.pk)
        return super(NoteInline, self).formfield_for_foreignkey(db_field, request, **kwargs)




相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签