Good morning my friends.
I have two forms that I use to insert data into the same models in different situations:
class SearchFormCompleto(forms.ModelForm):
class Meta:
model = Search
fields = (
'search_key',
'person',
'researched',
)
class SearchForm(forms.ModelForm):
class Meta:
model = Search
fields = '__all__'
exclude = ()
layout = Layout(
Fieldset("Responda com Calma.", Row('person', 'search_key'), Row('researched',),))
SearchItemFormSet = inlineformset_factory(Search, SearchItem,
exclude=('id',),
can_delete=True,
fields=('question', 'response'), extra=1)
These forms refer to the models below:
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
Which has a signals described below:
def post_save_search(sender, instance, created, **kwargs):
if created:
questions = Question.objects.all()
# search = Search.objects.filter(pk=instance.pk)
lista = []
# print(worksheet.nrows)
for question in questions:
print('search_id=', instance.pk, 'question_id=', question.pk)
lista.append(
SearchItem(search_id=instance.pk,
question_id=question.pk,
response=False,
)
)
SearchItem.objects.bulk_create(lista)
models.signals.post_save.connect(
post_save_search, sender=Search, dispatch_uid='post_save_search'
)
and the SeachItem model that has a Seach FK above:
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'),)
ordering = ('-question',)
def __str__(self):
return self.question.question
SeachItem also has an FK of Question:
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
My problem starts here:
When I use the view seach_create it registers the data in the Seach model and in turn the signals launches the questions in the SeachItem model (This is beautiful! At least for me because the situation requires it to be), see how the view is:
def seach_create(request):
if request.method == 'POST':
form = SearchForm(request.POST)
if form.is_valid():
print('<<<<==== FORM VALIDO ====>>>>')
new = form.save(commit=False)
new.save()
#form.save_m2m()
return HttpResponseRedirect('/pesquisa/listar' )
else:
print('<<<<==== AVISO DE FORMULARIO INVALIDO ====>>>>')
print(form)
return render(request, 'seach_create.html', {'form':form})
else:
context = {'form': SearchForm()}
return render(request, 'seach_create.html', context)
When I use the search_create view it does the same thing and then tries to register the formset fields, and that's where it returns the key duplicity error See how the view is:
def pesquisa_create(request):
success_message = 'The Search was edited correctly.'
if request.method == 'POST':
form = SearchForm(request.POST)
formset = SearchItemFormSet(request.POST)
if form.is_valid() and formset.is_valid():
with transaction.atomic():
receipt = form.save()
formset.instance = receipt
formset.save()
return redirect('/cliente/listar/')
else:
form = SearchForm()
# initial={'author': request.user}
formset = SearchItemFormSet()
forms = [formset.empty_form] + formset.forms
context = {'form': form, 'formset': formset, 'forms': forms}
return render(request, 'receipt_form.html', context)
My question is quite straightforward:
What should I do in search_create so it does not trigger the signals or to use get_or_create in formset?
Thank you.
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d0febe8b-f83d-42eb-9032-44c409e23754%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment