Это моя первая попытка использования наборов форм, и я застрял с этой ошибкой. Куда я иду не так? Я не до конца понимаю, как это работает. Я думаю, что моя форма ожидает пользователя, но я не знаю, что с ней делать. Спасибо за любую помощь!
ошибка:
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)
форма:
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>