1

I am newbie inDjango/Python I've started to implement admin forms and try to personnalize behavior

I would like to add a link on a specific change_form (not all change forms) that will open a new windows that allow the user to enter data in another change_form without leaving the first change form

the 2 forms correspond to 2 models linked (related tables)

is-it possible with admin forms?

EDIT I think this post may be a solution Django Admin nested inline I will try to implement this solution but explaination about this solution or advices are still welcome...

EDIT2

so I manage to use the exemple using the same classes but when I try to implement with my own classes it doesnt work

EDIT 3

i speak alone... in fact, it was self.ide and not self.id because I specified my primary key in my class...

from django.db import models
from django.urls import reverse
from django.utils.safestring import mark_safe


class Visite(models.Model):
    vis_ide = models.AutoField(primary_key=True)
    vis_dat = models.DateField("Date de consultation")

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 link(self):
        if self.id:
            changeform_url = reverse('admin:ecrf_bilanbiologique_change', args=(self.id,))
            return mark_safe('<a href="{u}" target="_blank">Details</a>'.format(u=changeform_url))
        return ''
    link.allow_tags = True

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é")
    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)

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

class ExamenBiologiqueInline(admin.StackedInline):
    model = ExamenBiologique

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

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

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


admin.site.register(Visite, VisiteAdmin)
admin.site.register(BilanBiologique, BilanBiologiqueAdmin)

enter image description here

SLATER
  • 613
  • 9
  • 31

0 Answers0