A
A
Alexander2017-08-01 12:17:04
Django
Alexander, 2017-08-01 12:17:04

How to pass a field of the associated model to the form?

Good day. I hope I asked the right question.
I have Product and Product Group models:

class ProductGroop(models.Model):
    ''' Модель товарной группы '''
    .....
    name_groop = models.CharField(max_length=54, blank=True, null=True, default=None, verbose_name='Товарная группа')
    .....

class Product(models.Model):
    ''' Модель товара '''
    .....
    groop = models.ForeignKey(ProductGroop, verbose_name='Товарная группа')
     ....

There is a product model in the order:
class ProductInOrder(models.Model):
    # Модель товара в заказе
    order = models.ForeignKey(Order, verbose_name='Заказ')
    product = models.ForeignKey(Product, verbose_name='Номенклатура')
    width = models.FloatField(verbose_name='Ширина')
    length = models.FloatField(verbose_name='Длина')
    amount = models.IntegerField(default=1, verbose_name='Количество')
    .....

And the form for adding a new product based on the ProductInOrder model:
class ProductInOrderForm(forms.ModelForm):
    class Meta:
        model = ProductInOrder
        fields = ['product', 'width', 'length', 'amount']

Question: how to pass the groop field from the Product model to the template.
Purpose: when adding a new product to the order, you must first select the Product Group, then the Product itself (associated with this Group), then enter the characteristics of the product (length/width/quantity).
Or tell me how it's done differently. (Groups about 4-6, Goods about 50 in total).
Knowledge is not enough - please do not kick.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ramon Yaskal, 2017-08-03
@ramonyaskal

The groop product entry (ForeignKey) stores the pk c of the group model which points to name_groop.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question