Skip to content

Instantly share code, notes, and snippets.

@CoutinhoElias
Created October 8, 2018 22:08
Show Gist options
  • Save CoutinhoElias/e39bf0010eaf5002323178b481ca46d3 to your computer and use it in GitHub Desktop.
Save CoutinhoElias/e39bf0010eaf5002323178b481ca46d3 to your computer and use it in GitHub Desktop.
A view não passa pelo meu manager
#Minha view
def addQuestions(self):
person = Person.objects.get(pk=1)
questions = Questions.objects.all()
for question in questions:
Pesquisa.objects.get_or_create(
search_key='092018',
person=person,
question=question,
response='I'
)
return HttpResponseRedirect('/admin/bolsa/pesquisa/')
#-------------------------------------------------------------------------------------------------------------------
# Meu modelo e manager dele
class PesquisaManager(models.Manager):
def add_question(self, search_key, question):
pesquisa, created = self.get_or_create(search_key=search_key, question=question)
if not created:
pesquisa.person = pesquisa.person
pesquisa.question = pesquisa.question
pesquisa.save()
return pesquisa
class Pesquisa(models.Model):
RESPOSTA_CHOICES = (
('V', 'Verdadeiro'),
('F', 'Falso'),
('I', 'Indefinido'),
)
search_key = models.CharField('Chave da pesquisa', max_length=10, db_index=True)
person = models.ForeignKey('person.person', related_name='Pessoa', on_delete=models.CASCADE)
question = models.ForeignKey('bolsa.Questions', related_name='Pergunta', on_delete=models.CASCADE,)
response = models.CharField('Resposta', max_length=1, choices=RESPOSTA_CHOICES, default='I')
participation_on = models.DateField('período da pesquisa', default=timezone.now)
created_on = models.DateTimeField('solicitado em', default=timezone.now)
objects = PesquisaManager()
class Meta:
verbose_name = 'Pesquisa'
verbose_name_plural = 'Pesquisas'
unique_together = (('search_key', 'question'),)
ordering = ('-participation_on',)
def __str__(self):
return self.question.question
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment