inline-formset

Django Inline for ManyToMany generate duplicate queries

三世轮回 提交于 2019-12-04 12:53:11
I'm experiencing some major performing issue with my django admin. Lots of duplicate queries based on how many inlines that I have. models.py class Setting(models.Model): name = models.CharField(max_length=50, unique=True) class Meta: ordering = ('name',) def __str__(self): return self.name class DisplayedGroup(models.Model): name = models.CharField(max_length=30, unique=True) position = models.PositiveSmallIntegerField(default=100) class Meta: ordering = ('priority',) def __str__(self): return self.name class Machine(models.Model): name = models.CharField(max_length=20, unique=True) settings

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

Deadly 提交于 2019-12-04 10:17:52
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): class Meta: model = Direction fields = ['text',] views.py def myview(request): Dirset = modelformset

Django Formset without instance

不问归期 提交于 2019-12-04 09:43:59
问题 In this Django Doc explain how to create a formset that allows you to edit books belonging to a particular author. What I want to do is: Create a formset that allows you to ADD new book belonging to a NEW author... Add the Book and their Authors in the same formset. Can you gime a light? thanks. 回答1: When you're instantiating the form and formset for the initial display, you don't need to provide an instance - so you will just get blank forms. When you pass in the data on POST, you can do the

How to create an inline formset for a reverse foreign key relationship

我们两清 提交于 2019-12-04 05:25:03
问题 I have a Property Model as follows = class Property(models.Model): property_type = models.CharField(max_length=255, default='Apartment') specifications = models.CharField(max_length=255, default='Basic') built_up_area = models.FloatField(max_length=6, null=False, default=0) total_area = models.FloatField(null=False, default=0) number_of_bedrooms = models.CharField(max_length=3, default=1) number_of_bathrooms = models.CharField(max_length=3, default=1) number_of_parking_spaces = models

Initial Data for Django Inline Formsets

这一生的挚爱 提交于 2019-12-04 03:41:44
I have put together a form to save a recipe. It makes use of a form and an inline formset. I have users with text files containing recipes and they would like to cut and paste the data to make entry easier. I have worked out how to populate the form portion after processing the raw text input but I cannot figure out how to populate the inline formset. It seems like the solution is almost spelled out here: http://code.djangoproject.com/ticket/12213 but I can't quite put the pieces together. My models: #models.py from django.db import models class Ingredient(models.Model): title = models

Django: How to change a field widget in a Inline Formset

天涯浪子 提交于 2019-12-04 01:00:01
I am new to Django and I think I am missing this in the docs. The problem is that in inline-formset I dont declare a form, just pass two models to construct it. I want to know how can I change a widget of a single field using inline formset? As of Django 1.6 , you can use the widgets parameter of modelformset_factory in order to customize the widget of a particular field: AuthorFormSet = modelformset_factory(Author, widgets={ 'name': Textarea(attrs={'cols': 80, 'rows': 20}) }) and therefore the same parameter for inlineformset_factory (which uses modelformset_factory ): AuthorInlineFormSet =

Django inlineformset - custom save method

女生的网名这么多〃 提交于 2019-12-03 17:13:09
This is my models.py class Invoices(models.Model): ... sum_w_vat = models.DecimalField(max_digits=7, decimal_places=2, default=0) sum_wo_vat = models.DecimalField(max_digits=7, decimal_places=2, default=0) sum_discount = models.DecimalField(max_digits=7, decimal_places=2, default=0) sum_vat = models.DecimalField(max_digits=7, decimal_places=2, default=0) sum_paid = models.DecimalField(max_digits=7, decimal_places=2, default=0) ... class InvoiceItems(models.Model): invoice = models.ForeignKey(Invoices) quantity = models.DecimalField(max_digits=9, decimal_places=2) unit = models.ForeignKey

How to clean a certain field in a InlineFormSet?

*爱你&永不变心* 提交于 2019-12-03 13:45:19
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. You can set the inline formset to use a form class, and then you can create a clean function for the field. In myapp/forms.py : class InlineFormsetForm(forms.Form) myfield =

Django admin: Inline straight to second-level relationship

寵の児 提交于 2019-12-03 07:24:51
I have a three-levels Invoice model which I'd like to display on Django's admin area... in a "kind of special " way. Allow me to provide a bit of background: Each Invoice is conformed by several SubInvoice (s), and each SubInvoice is conformed by several InvoiceItem (s), which contain a break down of the Products purchased by a customer. Logically speaking, it'd be something like this (hopefully the ascii art works) +---------- Invoice id=3 -----------+ | Full total: $100.00 | | | | +----- Sub Invoice id=1 -----+ | | | Subtotal $70 | | | | | | | | Item 1 in SubInv.1 | | | | Item 2 in SubInv.1

Django inline formsets and choicefields generate too many db queries

强颜欢笑 提交于 2019-12-03 07:16:51
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 (every time). For example: {% for form in formset %} {{ form.level }} {{ form.color }} {{ form.intensity }