排除通用CRUD视图中的字段 [英] Excluding fields in generic CRUD views

查看:65
本文介绍了排除通用CRUD视图中的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型名为 Domain ,如下所示:

I have a model named Domain which looks like this:

class Domain(models.Model):
    """
    Model for storing the company domains
    """
    user = models.ForeignKey(
        User
    )
    host = models.CharField(
        null=False, verbose_name="Host", max_length=128, unique=True
    )

我想使用Django的通用视图进行CRUD操作。该模型中有一个字段需要用户输入,但外键字段不需要任何用户输入。如何从该通用视图生成的表单中排除该字段,然后将其分配给当前验证用户的值。

I'd like to use Django's generic views for doing CRUD operations on this. There is one field in this model that needs user input but the foreign key field doesn't need any user input. How can I exclude that field from the form that my generic view generates but assign it the value of the current authenticated user.

谢谢。

推荐答案

看看Russel对 django-users组本周早些时候。

Have a look at Russel's answer to a similar question on the django-users group earlier this week.

引用答案:


表单和视图解决了不同的问题。

Forms and Views solve different problems.

该视图正在解决如何处理这个请求和
将其转换为响应?。该表单正在解决如何
将此请求中的POST数据转换为模型对象(或
更改为模型对象)的问题?。

The View is solving the problem of "how do I handle this request and convert it into a response?". The Form is solving the problem of "How do I convert the POST data in this request into a model object (or a change to a model object)?".

很大程度上,一个视图正在执行以下操作:

Very roughly, a view is doing the following:


  1. 查看获取请求

  2. 查看是否是GET或POST

  3. 如果是POST,查看请求表单将帖子转换为模型更改

  4. 表单返回成功或失败

  5. 查看响应表单的成功或失败。

  6. 查看返回响应。 >
  1. View gets a request
  2. View works out whether this is a GET or a POST
  3. If its a POST, View asks the Form to turn the Post into a model change
  4. Form returns success or failure
  5. View responds to the success or failure of the Form.
  6. View returns a response.

表单的功能是View的
功能的完整子集,因此,它完全是
可互换的内部组件。

The functionality of the Form is a complete subset of the functionality of the View -- and for this reason, it's a completely interchangable internal component.

现在,在简单的情况下,View可能会猜测所有
默认的窗体 - 所有它需要知道你是用一个Foo模型来处理
,而且它可以n构造一个默认的Foo ModelForm。
但是,如果您有更复杂的表单要求,则
将需要一个自定义表单。

Now, in simple situations, it's possible for a View to guess all the defaults for the form -- all it needs to know is that you're dealing with a Foo model, and it can construct a default Foo ModelForm. However, if you have more sophisticated form requirements, you're going to need a customized Form.

我们可以通过在View类上公开
ModelForm的所有选项来实现这一点;但是为了保持一切清洁,我们
保持ModelForm被隔离,并提供了View的方式,
指定要使用哪个Form类。

We could have implemented this by exposing all the options of ModelForm on the View class; but in order to keep everything clean, we kept the ModelForm isolated, and provided the View with a way to specify which Form class it's going to use.

所以 - 为了覆盖排除字段的用例,你定义一个
ModelForm,排除字段,然后让CreateView知道你要使用的
表单:

So - to cover your use case of excluding fields, you define a ModelForm that excludes the fields, then let the CreateView know the form you want to use:



class ModelForm(forms.ModelForm):


    class Meta:
        model = Campaign
        exclude = ('user', 'name', 'content_inlined')

class CreateCampaignView(CreateView):
    form_class = CampaignForm
    template_name = "forms/create.html"




我猜您说修改一个字段的值,您的意思是在保存新的
Campaign实例之前设置
的用户,名称和content_inlined的值;要做到这一点,您需要在
中注入一些额外的代码,其格式为:

I'm guessing when you say "fix a values for a field", you mean setting the values of user, name and content_inlined before you save the new Campaign instance; to do this, you need to inject some extra code into the form processing logic of the form:



class CreateCampaignView(CreateView):
    form_class = CampaignForm
    template_name = "forms/create.html"

    def form_valid(self, form):
        self.object.user = ... (something meaningful.. e.g., self.request.user)
        return super(CreateCampaignView, self).form_valid(form)




当表单有效时,这将覆盖默认行为,并设置
额外的值。然后,form_valid()的super()实现将
保存实例。

This overrides the default behavior when the form is valid, and sets the extra values. The super() implementation of form_valid() will then save the instance.

对于记录,这也可以通过覆盖save()$在ModelForm上的b $ b方法 - 但是,如果这样做,则丢失
请求对象,如果您尝试将
实例值设置为请求 - 敏感的。

For the record, this could also be done by overriding the save() method on the ModelForm -- however, if you do that, you lose the request object, which you will need if you're trying to set the instance values to something that is request-sensitive.

这篇关于排除通用CRUD视图中的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆