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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何修复modelformset错误:__init __()缺少1个必需的位置参数:'user'相关的知识,希望对你有一定的参考价值。

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

错误:

init()缺少1个必需的位置参数:'user'

模型:

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)

form:

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>
答案

我相信您想使用form_kwargs

form_kwargs
另一答案

您需要传递用户,所以您已重载formset = property_formset( queryset=PropertySubmission.objects.none(), form_kwargs='user': request.user ) 方法。但是据我了解,PropertyCreateKeepInTouchForm __init__modelform_factory函数不允许传递任意参数。

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

另一答案

最简单的方法是使用modelformset_factory

functools.partial

基本上,这会在您实际调用之前将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) ... 函数,这使Django可以初始化表单类本身。

以上是关于如何修复modelformset错误:__init __()缺少1个必需的位置参数:'user'的主要内容,如果未能解决你的问题,请参考以下文章

Python kivy:如何修复“TypeError:object.__init__() 不带参数”?

Django modelformset order_by 不工作

即使使用 __init__.py,如何修复“尝试在非包中进行相对导入”

如何修复 TypeError:main.__init__() 缺少 3 个必需的位置参数:'num1'、'num2' 和 'operator' [关闭]

如何修复pyinstaller中的no dbm clone found错误

Python线程:“RuntimeError:thread .__ init __()not called”