Skip to content

Instantly share code, notes, and snippets.

@CoutinhoElias
Last active January 23, 2019 01:54
Show Gist options
  • Save CoutinhoElias/572c8c54922d796e75620cee944990ff to your computer and use it in GitHub Desktop.
Save CoutinhoElias/572c8c54922d796e75620cee944990ff to your computer and use it in GitHub Desktop.
Adicionar em massa com unique_together
from django.db import models
from django.urls import reverse
class Question(models.Model):
LEVEL_CHOICES = (
('0', 'Dependencia'),
('1', 'Confianca'),
('2', 'Comprometimento'),
('3', 'Preditiva'),
)
question = models.CharField('Pergunta', max_length=200)
level = models.CharField('Nível', max_length=15,
choices=LEVEL_CHOICES, default='0')
class Meta:
verbose_name = 'Questão'
verbose_name_plural = 'Questões'
ordering = ('-level',)
def __str__(self):
return self.question
class Search(models.Model):
search_key = models.CharField(
'Período', max_length=200, db_index=False)
person = models.ForeignKey(
'core.client', related_name='Cliente', on_delete=models.CASCADE)
researched = models.CharField('Entrevistado', max_length=200)
participation_on = models.DateField(
'Período da pesquisa',
auto_now_add=True,
auto_now=False
)
created_on = models.DateTimeField(
'Solicitado em',
auto_now_add=True,
auto_now=False
)
class Meta:
verbose_name = 'Pesquisa'
verbose_name_plural = 'Pesquisas'
unique_together = (('search_key', 'person'),)
ordering = ('-participation_on',)
def get_absolute_url(self):
return reverse('person_client_detail', args=[str(self.pk)])
def __str__(self):
return self.search_key
def post_save_search(sender, instance, created, **kwargs):
if created:
questions = Question.objects.all()
search = Search.objects.filter(pk=instance.pk)
print(search)
lista = []
for question in questions:
try:
SearchItem.objects.get(
search_id=instance.pk,
question_id=question.pk,
response=False,
)
except SearchItem.DoesNotExist:
SearchItem.objects.get_or_create(
search_id=instance.pk,
question_id=question.pk,
response=False,
)
models.signals.post_save.connect(
post_save_search, sender=Search, dispatch_uid='post_save_search'
)
class SearchItem(models.Model):
RESPONSE_CHOICES = (
('V', 'Verdadeiro'),
('F', 'Falso'),
('I', 'Indefinido'),
)
search = models.ForeignKey(
'core.search', related_name='Periodo', on_delete=models.CASCADE, verbose_name="Período da Pesquisa")
question = models.ForeignKey(
'core.question', related_name='Pergunta', on_delete=models.CASCADE, verbose_name="Pergunta")
response = models.BooleanField('Resposta')
class Meta:
verbose_name = 'Pesquisa Detalhe'
verbose_name_plural = 'Pesquisas Detalhe'
unique_together = (('search', 'question', 'response'),)
ordering = ('-question',)
def __str__(self):
return self.question.question
@elyssonmr
Copy link

try:
    # Bloco antigo sem alterar nada
except SearchItem.DoesNotExist:
    continue # ou pass
    # Ignora o erro e pula para a próxima iteração do for

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment