O
O
ooker2020-11-11 00:24:45
Django
ooker, 2020-11-11 00:24:45

What is the correct way to use Foreing Key in Django ModelForm?

Good afternoon!
How to output data from a dependent table to a template.
I use CreateView, the main model is displayed well. Where can I find information about the ForeingKey relationship in ModelForm and display in the template. In django instructions, there is only a little on ModelChoiceFiled and is not clearly written.
Convenient in ListView

{%for obj in obl_list%}
{%for obj1 in obj%}

Is there such a thing in CreateView?

Model
class NewMaster(models.Model):
    CHOICE_FOR_CITY = (
        ('Город1', 'Город1'),
        ('Город2', 'Город2')
    )
    master_name = models.CharField(max_length=50, verbose_name="Ваше имя ", help_text="(ф о не обязательно)")
    master_city = models.CharField(choices=CHOICE_FOR_CITY, max_length=25, verbose_name='Выберите город', default=False)

class MasterChoiceRazdel(models.Model):
    master_haircut = models.BooleanField(verbose_name='ПАРИКМАХЕРСКАЯ', default=False)
    master_razdel_link = models.ForeignKey(NewMaster, on_delete=models.CASCADE, verbose_name='Выбор деятельности')


view
class MasterCreateProfileView(CreateView):
    form_class = NewMasterForm
    template_name = 'lkmastera/sozdat-sait.html'
    success_url = '/masters/'


Form
class NewMasterForm(forms.ModelForm):
    class Meta:
        model = NewMaster
        fields = '__all__'
        widgets = {
            'master_name': TextInput(attrs={'placeholder': 'Ваше имя', 'size': '40'}),
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question