Answer the question
In order to leave comments, you need to log in
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='Товарная группа')
....
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='Количество')
.....
class ProductInOrderForm(forms.ModelForm):
class Meta:
model = ProductInOrder
fields = ['product', 'width', 'length', 'amount']
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question