Как вы работаете с полями id форм, созданных с помощью inlineformset_factory Django?
Я пытаюсь сохранить несколько форм через inlineformset_factory, но он не сохраняет никаких данных. И набор форм также возвращает другие поля, например, поле ID. inlineformfactories также возвращает другие поля foreignkey. Что касается поля id, это специальное поле формы или поле модели django?
forms.py
class ImageFieldForm(forms.ModelForm):
class Meta:
model = ImageField
fields = ('image',)
class VideoFieldForm(forms.ModelForm):
class Meta:
model = VideoField
fields = ('video',)
class AdditionalFieldForm(forms.ModelForm):
class Meta:
model = AdditionalField
fields = ('icon', 'title', 'description',)
ImageFieldFormset = inlineformset_factory(Auction, ImageField, form=ImageFieldForm, extra=0, fields=('image',))
VideoFieldFormset = inlineformset_factory(Auction, VideoField, form=VideoFieldForm, extra=0, fields=('video',))
AdditionalFieldFormset = inlineformset_factory(Auction, AdditionalField, form=AdditionalFieldForm, extra=0, fields=('icon', 'title', 'description',), exclude=('id',))
views.py
def auction_create(request):
auction_form = AuctionForm()
image_formset = ImageFieldFormset()
video_formset = VideoFieldFormset()
additional_formset = AdditionalFieldFormset()
if request.method == 'POST':
auction_form = AuctionForm(request.POST, request.FILES)
image_formset = ImageFieldFormset(request.POST, request.FILES)
video_formset = VideoFieldFormset(request.POST, request.FILES)
additional_formset = AdditionalFieldFormset(request.POST, request.FILES)
if all([auction_form.is_valid(), image_formset.is_valid(), video_formset.is_valid(), additional_formset.is_valid()]):
new_auction = auction_form.save(commit=False)
new_auction.owner = request.user
new_auction.save()
new_auction.permissions.add(request.user)
user_permission = AuctionUserPermission.objects.get(user=request.user, auction=new_auction)
user_permission.can_edit = True
user_permission.can_delete = True
user_permission.can_add_admin = True
user_permission.save()
for image_form in image_formset:
new_image_form = image_form.save(commit=False)
new_image_form.auction = new_auction
new_image_form.save()
for video_form in video_formset:
new_video_form = video_form.save(commit=False)
new_video_form.auction = new_auction
new_video_form.save()
for additional_form in additional_formset:
new_additional_form = additional_form.save(commit=False)
new_additional_form.auction = new_auction
new_additional_form.save()
if new_auction.start_time:
pass
if new_auction.end_time:
pass
return redirect('auction', new_auction.slug)
else:
print(image_formset.errors)
context = {
'auction_form': auction_form,
'image_formset': image_formset,
'video_formset': video_formset,
'additional_formset': additional_formset,
}
return render(request, 'auctions/auction-create-update.html', context)
html форма