如何使用主键连接Django国家以创建自定义代码

2024-09-28 17:03:41 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是我的学员课。我想创建一个自定义代码,将country字段与自动生成的主键连接起来。到目前为止,我所做的一切都失败了,有人能帮我吗。代码的结构应该是国家第一,主键第二。我需要这个,因为当我打印报告时,代码应该出现在报告中

class Participant(models.Model):
    first_name = models.CharField(max_length=100)
    last_Name = models.CharField(max_length=100)
    country = CountryField()`enter code here`
    trainer = models.CharField(choices = trainer, max_length=100, )
    gender = models.CharField(choices= gender, max_length=50)

    title = models.CharField(choices= title_Choice, max_length=100, blank=True, null=True)
    date_of_birth = models.DateField(null=True, blank=True )


    contact_address = models.CharField(max_length=1000, blank=True, null=True)
    work_phone = models.CharField(max_length = 30, blank=True, null=True)
    fax_number = models.CharField(max_length = 100, blank=True, null=True)
    home_phone = models.CharField(max_length=30, blank=True, null=True)
    email = models.EmailField
    #previous_employment = models.CharField(max_length=100, blank=True, null=True)
    organization = models.ManyToManyField(Organization, blank=True)
    #role = models.ForeignKey(Role,  on_delete=models.CASCADE)

    education_level = models.CharField(choices = education_level_choice, max_length=100, blank=True, null=True)


    comments = models.CharField(max_length=1000, blank=True, null=True)

数据表的视图和HTML

views.py

class Index(LoginRequiredMixin, View):
    template = 'index.html'
    login_url = '/login/'


    def get(self, request):
        session =   Session.objects.all()
        participant = Participant.objects.all() 

        num_participant = Participant.objects.all().count()


        return render(request, self.template, {'participants': participant} )

index.html

 <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
                                        <thead>
                                            <th>First name</th>
                                            <th>Surname</th>
                                            <th>Country</th>
                                            <th>Gender</th>
                                            <th>Organization</th>
                                            <th>Trainer</th>
                                            <th>Session</th>
                                        </tr>
                                        </thead>

                                    <tbody>
                                    {% for participant in participants %}
                                       <tr>
                                          <td>{{ participant.first_name }}</td>
                                          <td>{{ participant.last_Name }}</td>
                                          <td>{{ participant.country }}</td>
                                          <td>{{ participant.gender }}</td>
                                          <td>{{ participant.get_organization }}</td>
                                          <td>{{participant.trainer}}</td>
                                          <td>{{ participant.host_participant.all }}</td>

Tags: 代码truemodelsallcountrynulllengthmax