B
B
bigtrouble2016-01-15 13:07:21
Django
bigtrouble, 2016-01-15 13:07:21

How to nest a form inside another form field?

Let's say we have an order and it has a buyer who refers to another table

class Order(model.Model)
    user = models.ForeignKey(User, verbose_name='Клиент *')
    buyer = models.ForeignKey(Buyer, verbose_name="Покупатель *", default=False)
    ...
class Buyer(model.Model)
    user = models.ForeignKey(User, verbose_name='Клиент')
    phone_number = models.CharField("Номер телефона *", blank=False, max_length=16)
    ...

class OrderForm(forms.ModelForm):
    class Meta:
        model = Order
        fields = ['buyer', ...]

class BuyerForm(forms.ModelForm):
    class Meta:
        model = Buyer
        fields = ['phone_number']

The form is something like this, but with this approach it turns into a ModelChoiceField and when rendered it becomes , there is no way to make it transform into for phone_number? In fact, it would be enough for BuyerForm to stand up instead of selecta

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nerevar_soul, 2016-01-15
@Nerevar_soul

As in the Dzhang admin panel. Next to the selection field is a plus, by clicking which the desired form appears in a pop-up window.

V
Vladimir, 2016-01-15
@vintello

there is such a thing forms.MultiWidget
something like this

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question