Django-管理员-在change_form中添加链接以重定向到另一个change_form-使用值设置字段

时间:2019-10-15 13:56:30

标签: python django admin

我打开了一个与此主题有关的帖子,但没有答案...

恢复: 我有一个带有链接的表格#1 当我单击此链接时,将打开一个新的#2表单,其中有一个子表单

现在,我希望表单#2(单击表单#1的链接后打开)的字段预先填充来自表单#1的值

所有这些操作都在Django的管理部分中进行

admin.py

from django.contrib import admin
from .models import Participante, Visite, Inclusion, BilanBiologique, ExamenBiologique

class ExamenBiologiqueInline(admin.TabularInline):
    model = ExamenBiologique
    extra = 0

class BilanBiologiqueAdmin(admin.ModelAdmin):
    inlines = [ExamenBiologiqueInline,]

class BilanBiologiqueLinkInline(admin.TabularInline):
    readonly_fields = ('examen', )
    model = BilanBiologique
    extra = 0
    fields = ('bio_ide', 'vis', 'bio_dat',)

class VisiteAdmin(admin.ModelAdmin):
    inlines = [BilanBiologiqueLinkInline,]

class InclusionAdmin(admin.ModelAdmin):
    readonly_fields = ('examen',)
    model = Inclusion
    fields = ('vis',('inc_tdr','examen'),)


admin.site.register(Participante)
admin.site.register(Visite, VisiteAdmin)
admin.site.register(BilanBiologique, BilanBiologiqueAdmin)
admin.site.register(Inclusion, InclusionAdmin)

models.py

from django.db import models
from django.urls import reverse
from django.utils.safestring import mark_safe
from django.core.validators import RegexValidator

class Participante(models.Model):

    pat_ide = models.AutoField(primary_key=True)
    pat_ide_prn_cse = models.CharField("Identifiant Princesse",
        max_length=14,
        validators = [
            RegexValidator(
                regex='^SP[M|F][0-9]{11}$',
                message= 'L\'identifiant doit être au format requis',
                code='invalid_participant_id'
            ),
        ], 
        unique=True
        )
    pat_inc_dat = models.DateField("Date d'inclusion")

    def __str__(self):
        return f"{self.pat_ide_prn_cse}"

class Visite(models.Model):
    vis_ide = models.AutoField(primary_key=True)
    pat = models.ForeignKey(Participante, verbose_name='Participante', related_name='visites', on_delete=models.CASCADE)
    vis_dat = models.DateField("Date de consultation")

    def __str__(self):
        return f"{self.pat.pat_ide_prn_cse} / {self.vis_dat}"

class BilanBiologique(models.Model):
    bio_ide = models.AutoField(primary_key=True)
    vis = models.ForeignKey(Visite, verbose_name='Visite', on_delete=models.CASCADE)
    bio_dat = models.DateField("Date de prélèvement")

    def __str__(self):
        return f"{self.bio_ide}"

    @property
    def date_visite(self):
        return self.vis.vis_dat

    date_visite.fget.short_description = 'Date de la visite'

    @property
    def participante(self):
        return self.vis.pat.pat_ide_prn_cse

    participante.fget.short_description = 'Participante'

class ExamenBiologique(models.Model):
    bio_exa_ide = models.AutoField(primary_key=True)
    bio = models.ForeignKey(BilanBiologique, verbose_name='Bilans', related_name='examens',on_delete=models.CASCADE)
    bio_exa_cod = models.IntegerField("Type d'examen")
    bio_exa_res_num = models.FloatField("Résultat numérique", null=True, blank=True)
    bio_exa_res_mod = models.IntegerField("Résultat modalité", null=True, blank=True)
    bio_exa_uni = models.IntegerField("Unité", null=True, blank=True)
    bio_exa_val_inf = models.FloatField("Limite inférieure", null=True, blank=True)
    bio_exa_val_sup = models.FloatField("Limite supérieure", null=True, blank=True)

    def __str__(self):
        return f"{self.bio_exa_ide}"

class Inclusion(models.Model):
    readonly_fields = ('examen',)

    inc_ide = models.AutoField(primary_key=True)
    vis = models.ForeignKey(Visite, verbose_name='Visite', related_name='inclusions', on_delete=models.CASCADE)
    inc_tdr = models.IntegerField("Test rapide Determine® VHB (AgHBs)")

    def examen(self):
        changeform_url = reverse('admin:ecrf_bilanbiologique_add')
        return mark_safe('<a href="{u}" target="_blank">Ajouter un bilan</a>'.format(u=changeform_url))
    examen.short_description =''
    examen.allow_tags = True

    def __str__(self):

        return f"{self.vis} / {self.inc_ide}"

0 个答案:

没有答案