Answer the question
In order to leave comments, you need to log in
How to display the same field multiple times in a form?
Hello, I have been thinking about the task for several days now, how to display the categories field several times in the form?
I have a model
#models.py
class СategoryPrice(models.Model)
performer = ForeignKey(Performer)
category = ForeignKey(Category)
price = Charfield(max_length=5)
class Category(models.Model):
name = models.CharField(max_length=64)
def __str__(self):
return self.name
class Performer(models.Model):
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
blank=True,
unique=True
)
company_name = models.CharField(max_length=30)
categories = models.ManyToManyField(Category, throught=CategoryPrice)
class AddPerformerForm(ModelForm):
class Meta:
model = Performer
fields = '__all__'
exclude = ['user']
Answer the question
In order to leave comments, you need to log in
In question 2, the doubt is correctly noted, if you only need to save the prices of the artist for a certain category, it is
easier
to
use
: situations are overkill.
This is from what is seen from the presented example. Perhaps the full project imposes other requirements.
Well, if, nevertheless, the first option:
1. Display one model field several times in the form - it means that the form needs to be customized and concise fields = '__all__' is indispensable:
class AddPerformerForm(ModelForm):
category1 = прописываем обычное поле формы, дублирует поле модели (max_length, и т.п.)
category2 = прописываем обычное поле формы еще раз
class Meta:
model = Performer
fields = ('performer`, 'price`) # вместо "все", пишем остальные нужные поля кроме категорий
# exclude тогда не требуется
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question