How to differentiate variable (inputs) in the same Django form

1

Good morning! I have the following question:

I have a Person class, of which I can have 3 types of person (Victim, Aggressor and Witness) in the same form ... how would I do to set the variables so that when saving to the bank are 3 different attributes

ex.

Crimes Against Life Form

VICTIM

fom_vida.name

AGRESSOR

form_vida.name

WITNESS

form-vida.name

This in the same form, that is, I want to differentiate the variables!

Hello, Renan. I do not understand your answer:

class ForensicAgressor(models.Model):
    name = models.CharField(max_length=100, blank=True, null=True, verbose_name='Nome')
    birth = models.DateTimeField(blank=True, null=True, verbose_name='Data de Nascimento')

class ForensicVitima(models.Model):
    name = models.CharField(max_length=100, blank=True, null=True, verbose_name='Nome')
birth = models.DateTimeField(blank=True, null=True, verbose_name='Data de Nascimento')


class ForensicPessoa(models.Model):
    agressor = models.ForeignKey(ForensicAgressorn)
    vitima = models.ForeignKey(ForensicVitima)

See above how it is, what would I do in the same Form to set the input of each object?

    
asked by anonymous 12.05.2016 / 16:43

2 answers

1
#! /usr/bin/python
# -*- coding: utf-8 -*-

from django.db import models
from django import forms

# Author: Ruben Alves do Nascimento <[email protected]>


class Pessoa(models.Model):
    VITIMA = 1
    AGRESSOR = 2
    TESTEMUNHA = 3

    TIPOS_PESSOA = (
        (VITIMA, 'Vítima'),
        (AGRESSOR , 'Agressor'),
        (TESTEMUNHA, 'Testemunha'),
    )

    nome = models.CharField(max_length=30, help_text='Nome na pessoa')
    tipo = models.IntegerField(choices=TIPOS_PESSOA, help_text='Tipo de pessoa')


class Vitima(Pessoa):
    class Meta:
        proxy = True

    def save(self, *args, **kwargs):
        self.tipo = self.VITIMA
        super(Vitima, self).save(*args, **kwargs)

class Agressor(Pessoa):
    class Meta:
        proxy = True

    def save(self, *args, **kwargs):
        self.tipo = self.AGRESSOR
        super(Agressor, self).save(*args, **kwargs)

class Testemunha(Pessoa):
    class Meta:
        proxy = True

    def save(self, *args, **kwargs):
        self.tipo = self.TESTEMUNHA
        super(Testemunha, self).save(*args, **kwargs)


class VitimaForm(forms.ModelForm):
    class Meta:
        model = Vitima
        exclude = ('tipo', )

class AgressorForm(forms.ModelForm):
    class Meta:
        model = Agressor
        exclude = ('tipo', )

class TestemunhaForm(forms.ModelForm):
    class Meta:
        model = Testemunha
        exclude = ('tipo', )


######## agora, supondo que a pessoa submeteu o form com a variavel nome
# Para registrar uma vítima
nome = 'Sou vitima'
pessoa = Vitima(nome=nome)
pessoa.save()

# Para registrar um agressor
nome = 'Sou agressor'
pessoa = Agressor(nome=nome)
pessoa.save()

# Para registrar uma testemunha
nome = 'Sou testemunha'
pessoa = Testemunha(nome=nome)
pessoa.save()
    
12.05.2016 / 17:42
1

You can have the same Form 3 buttons to handle different submissions.

<button type="submit" value="some_value" name="Agressor">some_value</button>
...
<button type="submit" value="some_value" name="Vitima">some_value</button>
...
<button type="submit" value="some_value" name="Pessoa">some_value</button> 

In your view you can differentiate the different POST's by doing:

if request.POST.get('Agressor'):
...
if request.POST.get('Vitima'):
...
if request.POST.get('Pessoa'):
    
24.06.2016 / 03:52