带有预填充字段的form_add

时间:2019-10-17 15:52:42

标签: python django

我有一个模型,该模型分为3类:Visit,Inclusion,BilanBiologique和ExamenBiologique

  • 包含内容链接到Visite(OneToOne)
  • BilanBiologique链接到Visite(ForeignKey)
  • ExamenBiologique链接到BilanBiologique(ForeignKey)
  • ExamenBiologique是BilanBiologique的子形式

我使用Django管理表单 我尝试将“包含”管理表单自定义为

  • 具有一个将用户重定向到BilanBiologique的链接 和
  • 我希望在“ vis”字段(即ForeignKey)中预先填充相应的值(我可以从“包含”中检索该值)

我设法在包含类中使用方法“示例”显示从包含到BilanBiologique的链接 我考虑过要使用GET方法,但老实说我迷路了...我不知道如何进行...

models.py

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}"

admin.py

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

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','examen')
    form = BilanBiologiqueFormAdmin

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

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


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

0 个答案:

没有答案