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

Asked

Viewed 361 times

1

good morning! I have the following question:

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

ex.

Form of crimes against life

VICTIM

fom_vida.name

AGGRESSOR

form_vida.name

WITNESS

form-life.name

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

Hello Renan. I didn’t 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 as it is, as I would in the same Form to set the value (input) of each object?

  • If victim, perpetrator and witness are objects of the same class, it may be necessary to add an attribute to the class, for example, role ("role"). Hence a person may have name = "fulano" and role = "agressor", another may have name = "beltrano" and role = "testemunha" etc..

  • Hello Renan, I did not understand your answer, see above, I tried to rephrase the question

  • Hello, Sara. I never wore Django. I was wondering how the app would distinguish a victim from an attacker, for example, just by looking through the database. It’s just that you mentioned a class person, but I hadn’t seen that there are three distinct classes.

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()

1

You can have in the same Form 3 buttons to handle the 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'):
  • In fact the @Renan response makes sense. A Person has an aggressor and a victim?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.