0

我一直试图保存一个模型的实例,该实例从一个表单中获取另一个模型的实例作为外键。

楷模

class Customer(models.Model):
    owner = models.ForeignKey(User)
    custname = models.CharField() 

class Appointment(models.Model):
    user = models.ForeignKey(User)
    start = models.DateTimeField()
    end = models.DateTimeField()
    customer = models.ForeignKey(Customer)

形式

class AppointmentForm(forms.Form):
    basedate = forms.DateField()
    start = forms.TimeField(widget=forms.Select())
    end = forms.IntegerField(widget=forms.Select())
    customer = forms.ModelMultipleChoiceField(queryset=Customer.objects.all())

我无法在通用 FormView 中工作的方法:

def form_valid(self, form):
    if form.is_valid():
        appointment = Appointment()
        appointment.user = self.request.user
        basedate = form.cleaned_data['basedate']
        start = form.cleaned_data['start']
        duration = form.cleaned_data['end']
        appointment.start = datetime.datetime.combine(basedate, start)
        appointment.end = appointment.start + datetime.timedelta(minutes=duration)
        appointment.save()
        return super(AppointmentCreate, self).form_valid(form)

我应该在最后一种方法中添加什么来从表单中读取外键客户,然后将其传递给约会?是否有任何过滤方式,以便在表单中只出现属于 request.user 的客户?

非常感谢您的帮助。

4

2 回答 2

1

像这样的东西应该工作。有几件事:

1)我将表单字段更改为 ModelChoiceField 而不是多项选择。您需要使用 ModelChoiceField 来显示关系。我从 MultipleChoice 更改了它,因为根据您的模型,您只想保存一个选择。您可以在此处阅读有关 ModelChoiceFields 的更多信息:https ://docs.djangoproject.com/en/dev/ref/forms/fields/

2)在您的表格中,我将选择查询更改为customer = forms.ModelChoiceField(queryset=Customer.objects.filter(owner=request.user). 这将仅过滤特定用户的客户。

表格.py

class AppointmentForm(forms.Form):
    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop("request")
        super(AppointmentForm, self).__init__(*args, **kwargs)

    basedate = forms.DateField()
    start = forms.TimeField(widget=forms.Select())
    end = forms.IntegerField(widget=forms.Select())
    customer = forms.ModelChoiceField(queryset=Customer.objects.filter(owner=request.user))

视图.py

def form_valid(self, form):
    if request.method=='POST':
        form = AppointmentForm(request.POST, request=request)
        if form.is_valid():
            appointment = Appointment()
            appointment.user = self.request.user
            basedate = form.cleaned_data['basedate']
            start = form.cleaned_data['start']
            duration = form.cleaned_data['end']
            appointment.customer = form.cleaned_data['customer']
            appointment.start = datetime.datetime.combine(basedate, start)
            appointment.end = appointment.start + datetime.timedelta(minutes=duration)       
            appointment.save()
            return super(AppointmentCreate, self).form_valid(form)
    else:
        form = AppointmentForm()
于 2013-01-11T20:34:09.487 回答
0

最后我做到了。关键是要覆盖views.py中FormView类的get方法,而不是修改forms.py中的init :

表格.py:

class AppointmentForm(forms.Form):
    basedate = forms.DateField()
    start = forms.TimeField(widget=forms.Select())
    end = forms.IntegerField(widget=forms.Select())
    customer = forms.ModelChoiceField(queryset=Customer.objects.all())
    ...

视图.py:

    def get(self, request, *args, **kwargs):
        """
        Handles GET requests and instantiates a blank version of the form.
        """
        choices_start, choices_duration = self._get_choices()
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        form.fields['start'].widget=forms.Select(choices=choices_start)
        form.fields['end'].widget=forms.Select(choices=choices_duration)
        form.fields['customer'].queryset=Customer.objects.filter(owner=request.user)
        return self.render_to_response(self.get_context_data(form=form))

@Dan:非常感谢您为帮助我所做的努力。

于 2013-01-14T18:47:27.917 回答