如何修复modelformset错误:__init __()缺少1个必需的位置参数:'user'

时间:2019-10-29 21:52:49

标签: django django-forms formset

这是我第一次尝试使用表单集,但我被此错误困扰。我要去哪里错了?我还不完全了解这是如何工作的。我认为我的表单正在期待一个用户,但是我不知道该怎么做。感谢您的帮助!

错误:

init ()缺少1个必需的位置参数:“用户”

型号:

class PropertySubmission(models.Model):

BANNER_CHOICES = (
    ('NB', 'No Banner'),
    ('FL', 'For Lease'),
    ('FS', 'For Sale'),
    ('NL', 'New Listing'),
    ('SD', 'Sold'),
    ('LD', 'Leased'),
    ('RD', 'Reduced'),
    ('NP', 'New Price'),
    ('SC', 'Sold Conditionally'),
    ('CB', 'Custom Banner'),
)

image = models.ImageField(upload_to=user_directory_path, blank=True)
mls_number = models.CharField(max_length=8, blank=True)
headline = models.CharField(max_length=30)
details = RichTextField()
banner = models.CharField(max_length=2, choices=BANNER_CHOICES)
author = models.ForeignKey(User, on_delete=models.CASCADE)
date_posted = models.DateTimeField(default=timezone.now)
date_modified = models.DateTimeField(default=timezone.now)
program_code = models.ManyToManyField(Program)
product = models.ForeignKey('Product', on_delete=models.SET_NULL, null=True)
production_cycle = models.ManyToManyField('ProductionCycle')
shell = models.ForeignKey('Shell', on_delete=models.SET_NULL, null=True)
card_delivery_instructions = models.CharField(max_length=1000, blank=True)
card_delivery_instructions_image = models.ImageField(upload_to=card_delivery_instructions_image_path, blank=True)

表格:

class PropertyCreateKeepInTouchForm(forms.ModelForm):

class Meta:
    model = PropertySubmission
    fields = ['headline','details','banner','image','mls_number','program_code']
    help_texts = {
        'details': '110 characters maximum',

    }

def clean(self):
    cleaned_data = super().clean()
    image = cleaned_data.get("image")
    mls_number = cleaned_data.get("mls_number")
    program_code = cleaned_data.get("program_code")

    if mls_number == '' and image is None:
        # Only do something if one field are valid so far.
        self.add_error('image', 'Please provide an image or MLS number')
        self.add_error('mls_number', 'Please provide an image or MLS number')
        raise forms.ValidationError("Please provide a number")

    if program_code is None:
        self.add_error('program_code', 'Please select one or more programs')
        raise forms.ValidationError("Please select one or more program")

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

    #self.fields['details'].widget = forms.Textarea(attrs={'rows':4, 'cols':15, 'maxlength':110})
    self.fields['details'].label = "Provide Feature Property Details:"
    self.fields['program_code'].widget = forms.CheckboxSelectMultiple()
    self.fields['program_code'].queryset = Program.objects.filter(client=user).filter(production_cycles__product_name__name='Keep In Touch').filter(production_cycles__submission_deadline__lt=timezone.now()+timedelta(days=30)).filter(production_cycles__submission_deadline__gt=timezone.now()).distinct()
    self.fields['program_code'].label = "Select Programs"
    self.fields['mls_number'].label = "number - to retrieve a photo of the property."
    self.fields['image'].label = "Property Photo"
    self.fields['banner'].label = "Select a banner"
    self.fields['headline'].label = "Provide a headline"

视图:

from django.forms import modelformset_factory
@login_required
def create_properties_keepintouch(request, num_forms):

property_formset = modelformset_factory(PropertySubmission, form=PropertyCreateKeepInTouchForm, extra=1)

formset = property_formset(queryset=PropertySubmission.objects.none())

return render(request, 'programs/property_submission_create_formset.html', {'formset': formset, 'num_forms':num_forms})

模板:

<form class="form-horizontal" method="POST" action="">
{% csrf_token %}
{{ formset.management_form }}
{% for form in formset %}
<div class="row form-row spacer">


    <div class="col-6">
        <hr>
        <div class="input-group">

            {{ form.media }}
            {{form.as_p}}


        </div>
    </div>
</div>
{% endfor %}
<div class="row spacer">
    <div class="col-4 offset-2">
        <button type="submit" class="btn btn-block btn-primary">Create</button>
    </div>
</div>
</form>

3 个答案:

答案 0 :(得分:1)

我相信您想使用form_kwargs

formset = property_formset(
    queryset=PropertySubmission.objects.none(), 
    form_kwargs={'user': request.user}
)

答案 1 :(得分:0)

您需要传递用户,因此您已经重载了PropertyCreateKeepInTouchForm __init__方法。但是据我了解,modelform_factorymodelformset_factory函数不允许传递任意参数。

据我所知,您将需要手动创建表单集,而不要使用工厂功能。

答案 2 :(得分:0)

最简单的方法是使用functools.partial

from functools import partial

def create_properties_keepintouch(request, num_forms):

    form = partial(PropertyCreateKeepInTouchForm, request.user)
    property_formset = modelformset_factory(PropertySubmission, form=form, extra=1)
    ...

基本上,这会将user参数传递给__init__函数,然后再实际调用它,这使Django可以初始化表单类本身。