inline-formset

Make inlineformset in django required

做~自己de王妃 提交于 2020-01-14 14:13:13
问题 I am new to django (until now, I used the symfony PHP Framework). My problem is this: I have a model Event and model Date. The Date has a foreign key to Event, so that an Event may (or should) have one or more Dates. Now I want to have a form to create Events and this form should include a subform for adding one corresponding Date (more dates should be added later, if the Event has more than one date). I used the inlineformset to realize the Date subform, but if no Date is being entered, no

Django inline formsets and choicefields generate too many db queries

笑着哭i 提交于 2020-01-12 05:32:10
问题 I have a model with a number of foreign key fields, e.g. model Product with fields 'type', 'level', 'color', 'intensity' (just a generic example). I then have a page to edit all products of a given type using a Type form with the products as an inline formset with the option to add additional products inline using extra=10 . The thing i find very strange is that each time when i output one of the foreign key choice fields on the template Django queries the database for the get the options

Django inline formset for each object delete

风格不统一 提交于 2020-01-07 11:02:14
问题 if formset.is_valid(): values = formset.save(commit=False) for v in values: v.correct = True v.save() How can I delete objects where delete is checked? Thanks 回答1: First, in your formset, mark your delete checkboxes with name="form-0-DELETE" , name="form-1-DELETE" , etc. Then in your if form.is_valid and formset.is_valid iterate over the values marked for delete. for delete_value in formset.deleted_objects: delete_value.delete() Then continue with saving your formset 来源: https://stackoverflow

Django inline formset for each object delete

不问归期 提交于 2020-01-07 11:01:46
问题 if formset.is_valid(): values = formset.save(commit=False) for v in values: v.correct = True v.save() How can I delete objects where delete is checked? Thanks 回答1: First, in your formset, mark your delete checkboxes with name="form-0-DELETE" , name="form-1-DELETE" , etc. Then in your if form.is_valid and formset.is_valid iterate over the values marked for delete. for delete_value in formset.deleted_objects: delete_value.delete() Then continue with saving your formset 来源: https://stackoverflow

Linking to other Foreign Model with DetailView and Formset in Django

寵の児 提交于 2020-01-04 05:55:09
问题 I am new to django-formset. I have been trying to find a way to link the models at formset (Model_CustomerCart and Model_CustomerCartItem) with the other model named Model_ItemPrice. Such that with DetailView, the html page can display a list of items and also their corresponding price. Does anyone know a way to make this happens? My code is below. models.py class Model_ItemIndex(models.Model): item_name = models.CharField(max_length = 50, null = True, blank = False) class Model_ItemPrice

Nested and Segmented Crispy Layouts

╄→尐↘猪︶ㄣ 提交于 2020-01-02 13:44:50
问题 TLDR Question: How do you make one crispy form with a ¿segmented?(not sure if this is considered inline) layout with multiple models(some related, some not). I am trying to understand several things in Django: forms, formsets, nested forms, and crispy, and I've been at it for a while, and feel I am close, just need someone to help connect the dots. I'm not sure how to accomplish it without crispy, so I started down this path thinking crispy was the solution. Please correct if I am wrong,

Model Formset - By Default model formset is rendering one extra field (2 fields in total)

随声附和 提交于 2020-01-01 14:45:13
问题 My model formset without even defining "extra" parameters in modelformset_factory is rendering one extra field in the template. I have tried many variations but it didn't work. If I print the form (the model form) on the command line it just prints a single form field as required but on model formset it prints 2 by default. Here is my code. models.py class Direction(models.Model): text = models.TextField(blank=True, verbose_name='Direction|text') forms.py class DirectionForm(forms.ModelForm):

How to clean a certain field in a InlineFormSet?

夙愿已清 提交于 2020-01-01 05:10:10
问题 I need to clean a specific field in an inline formset, and I can't figure out how to do it. I've tried with the formsets def clean(self) method but don't know where to save the cleaned value. If I try to set the cleaned value to forms[0].data['field'] I get "This QueryDict instance is immutable" error. In "normal" forms it works by using the def clean_fieldXY(self) method in which I return cleaned_value . Please help. 回答1: You can set the inline formset to use a form class, and then you can

Setting model user to current user when using inlineformset_factory in Django

徘徊边缘 提交于 2019-12-25 09:26:22
问题 I've hit a roadblock that I've spent hours trying to overcome, and I would appreciate some guidance. I have two models: Listings and Addresses, with the following structure: class Listings(models.Model): created_date = models.DateTimeField(auto_now_add=True) pub_date = models.DateTimeField(auto_now_add=True) address = models.ForeignKey(Addresses) user = models.ForeignKey(User, on_delete=models.CASCADE) is_active = models.BooleanField(default=1) class Addresses(models.Model): address1 = models

Inline formset returns empty list on save?

我们两清 提交于 2019-12-25 01:32:23
问题 When I try to save my inline formset it just returns an empty list and no changes are reflected in the database. I have tried doing it with no option and commit=False but they both have the same result. I know there is data because I printed the formset as a table, and I know it is valid because the property is_valid() method returns true. Here is the code: def edit(request): if request.method == 'POST': print(request.POST) form = TombstoneForm(request.POST) print(form.is_valid()) t = form