Skip to content

Instantly share code, notes, and snippets.

@CoutinhoElias
Created October 8, 2018 22:22
Show Gist options
  • Save CoutinhoElias/7e8c9841aff95f3b1d9f6228020854a0 to your computer and use it in GitHub Desktop.
Save CoutinhoElias/7e8c9841aff95f3b1d9f6228020854a0 to your computer and use it in GitHub Desktop.
08102018_1914
#View para post e get do formset
class QuestionsDetailWiew(TemplateView):
template_name = 'queryes.html'
def get_formset(self, clear=False):
QuestionsFormSet = modelformset_factory(
Pesquisa, fields=('response',), can_delete=False, extra=0
)
if clear:
formset = QuestionsFormSet(
queryset = Pesquisa.objects.filter(search_key='092018')
)
else:
formset = QuestionsFormSet(
queryset = Pesquisa.objects.filter(search_key='092018'),
data=self.request.POST or None
)
return formset
def get_context_data(self, **kwargs):
context = super(QuestionsDetailWiew, self).get_context_data(**kwargs)
context['formset'] = self.get_formset()
return context
def post(self, request, *args, **kwargs):
formset = self.get_formset()
context = self.get_context_data(**kwargs)
if formset.is_valid:
formset.save()
context['formset'] = self.get_formset(clear=True)
return self.render_to_response(context)
questions = QuestionsDetailWiew.as_view()
#model em questão
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 dapesquisa', 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