Hello,
I am newbie in Django and Python so hope you will be indulgent
I've read and read again the Django documentation but for now, some concept 
still are very abstract for me...
 
I have a database and I use ModelForm and I try to customize this admin forms 
I currently have a main form with a subform and both forms are customized

I noticed 'abnormal' behavior (unwanted): even when no entry is made in the 
subform, empty record is stored in the table corresponding to the subform in 
the database
how to prevent this?
thanks in adavnce fory your help

*******************************

Bonjour,

Je suis débutant en Django et Python donc j'espère que vous serez indulgent
je précise que je lis et relis la doc Django mais pas mal de chose reste 
abstraites pour moi pour le moment


j'ai mis en place une base de données et des formulaires admin via ModelForm 
que j'essaie de personnaliser
j'ai donc un formulaire et un sous-formulaire, les deux formulaires sont 
personnalisés

j'ai noté un comportement 'anormal' (non souhaité) : même lorsqu'aucune saisie 
n'est réalisée dans le sous-formulaire, un enregistrement vide est créé dans la 
table correspondante au sous-formulaire
comment faire pour empêcher cela?
d'avance merci pour votre aide

models.py : 

class SuiviAppuiCommunitaire(SafeDeleteModel):
        """ A class to create a Community instance. """
 
        _safedelete_policy = SOFT_DELETE_CASCADE
        com_ide = models.AutoField(primary_key=True)
        pat = models.ForeignKey(Participante, verbose_name='Participante', 
related_name='community',
                on_delete=models.CASCADE)
        com_dat = models.DateField("Date de la demande",null=True,blank=True)
        com_mot = models.IntegerField("Motif", 
max_length=1,null=True,blank=True)
        com_det = models.CharField("Détails", 
max_length=255,null=True,blank=True)
        com_com = models.CharField("", max_length=255,null=True,blank=True)
        log = HistoricalRecords()
 
        class Meta:
 
                db_table = 'crf_com'
                verbose_name_plural = '5.1-Appuis / Suivis communautaires(COM)'
                ordering = ['pat', 'com_dat','com_ide']
 
        def __str__(self):
 
                return f"{self.com_ide} / {self.pat.pat_ide_prn_cse} / 
{self.com_dat}"
         @property
        def participante(self):
 
                return self.pat.pat_ide_prn_cse
 
        participante.fget.short_description = 'Participante'
 
 class InterventionCommunautaire(SafeDeleteModel):
        """ A class to create a Community instance. """
 
        _safedelete_policy = SOFT_DELETE_CASCADE
        com_int_ide = models.AutoField(primary_key=True)
        com = models.ForeignKey(SuiviAppuiCommunitaire, verbose_name='Suivi / 
appui communautaire', related_name='interventions',
                on_delete=models.CASCADE)
        com_int_dat = models.DateTimeField("Date", null=True, blank=True)#      
com_int_edu = models.CharField("Paire-éducatrice", max_length=1)
        com_int_edu = models.ForeignKey(Personnel, verbose_name='Personnel', 
related_name='communitiInterventions', on_delete=models.PROTECT, null=True, 
blank=True)
        com_int_typ = models.IntegerField("Type")
        com_int_vue = models.IntegerField("La participante a-t-elle été 
contactée/vue ?")
        com_int_rea = models.CharField("si oui, intervention réalisées", 
max_length=255)
        com_int_rdv = models.IntegerField("Avez-vous fixé un nouveau 
rendez-vous ?")
        com_int_rdv_dat = models.DateTimeField("Si oui, date du prochain 
rendez-vous", null=True, blank=True)
        com_int_rdv_lie = models.IntegerField("Lieu")
        log = HistoricalRecords()
 
        class Meta:
 
                db_table = 'crf_com_int'
                verbose_name_plural = 'Interventions communautaires'
                ordering = ['com_int_dat','com', 'com_int_ide']
 
        def __str__(self):
                return f"{self.com_int_ide} / {(self.com_int_dat)}"

admin.py : 

class InterventionCommunautaireFormAdmin(forms.ModelForm):
    """ A class to customised the admin form for community interventions. """
 
    TYPES = Thesaurus.options_list(52)
    VUES = Thesaurus.options_list(1)
    NOUVEAUX = Thesaurus.options_list(53)
    LIEUX = Thesaurus.options_list(44)
    com_int_dat = forms.DateTimeField(label="Date", required=False)
    com_int_typ = forms.ChoiceField(label="Type", widget=forms.Select, 
choices=TYPES)
    com_int_rea = forms.CharField(label="si oui, interventions réalisées" , 
widget=forms.Textarea, required=False)
    com_int_vue = forms.ChoiceField(label="La participante a-t-elle été 
contactée/vue ?", widget=forms.Select, choices=VUES)
    com_int_rdv = forms.ChoiceField(label="Avez-vous fixé un nouveau 
rendez-vous ?", widget=forms.Select, choices=NOUVEAUX)
    com_int_rdv_dat = forms.DateTimeField(label="Si oui, date du prochain 
rendez-vous", required=False)
    com_int_rdv_lie = forms.ChoiceField(label="Lieu", widget=forms.Select, 
choices=LIEUX)
 
 class InterventionCommunautaireInline(admin.TabularInline):
 
    model = InterventionCommunautaire
    """pour qu'il n y ait pas systématiquement 3 lignes de sous-formulaire en 
base pour une fiche suivi communautaire"""
    extra = 1 
    form = InterventionCommunautaireFormAdmin
 
 class SuiviAppuiCommunitaireFormAdmin(forms.ModelForm):
    """ A class to customised the admin form for Community. """
 
    MOTIFS = Thesaurus.options_list(51)
    com_dat = forms.DateField(label="Date de la demande",required=False)
    com_mot = forms.ChoiceField(label="Motif", widget=forms.Select, 
choices=MOTIFS,required=False)
    com_det = forms.CharField(label="Détails",required=False)
    com_com = forms.CharField(label="", widget=forms.Textarea,required=False)
 class SuiviAppuiCommunitaireAdmin(SimpleHistoryAdmin):
 
    list_display = ('com_ide','participante', 'com_dat',)
    search_fields = ('participante','com_dat')
    fieldsets = (
        ('SUIVI / APPUI COMMUNAUTAIRE', {
            'classes': ('opened',),
            'fields': ('pat', )
        }),
        ('Demande de suivi / appui communautaire', {
            'classes': ('opened',),
            'fields': (('com_dat','com_mot','com_det',), )
        }),
        ('Problèmes rencontrés par la participante / Commentaires libres', {
            'classes': ('opened',),
            'fields': (('com_com',), )
        }),
    )
    form = SuiviAppuiCommunitaireFormAdmin
    inlines = [
        InterventionCommunautaireInline,
    ]



-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/e7cb32cb-514c-4855-8a3b-5102fb083726%40googlegroups.com.

Reply via email to