Hi, people!
I want to write a view, which save all my data, entered to form.(like
contrib.admin)
But I got a problem.
How to get a pk value of parent form, when it's just saved(form is
continue editing)?
I have such models:
######
class Question(models.Model):
number = models.IntegerField()
question = models.TextField()
class Choice(models.Model):
question = models.ForeignKey('Question')
description = models.CharField(max_length=255)
class QuestionForm(forms.ModelForm):
class Meta:
model = Question
ChoiceQuestionFormSet = inlineformset_factory
(Question,Choice,extra=1,can_delete=True)
#######
def cat4_view(request):
if request.method == 'POST':
uform = QuestionForm(request.POST,prefix='question',)
choice_formset = ChoiceQuestionFormSet(request.POST,
request.FILES, instance=uform.instance,prefix='choice',)
#
#uform.insnace is always None !
#
print dir(uform)
print uform.instance
print dir(uform.instance)
print uform.instance.pk
print uform.instance.id
print uform.instance.question
assert False
if uform.is_valid():
number = uform.cleaned_data['number']
description = uform.cleaned_data['question']
try:
q = Question.objects.get(number = number)
q.question = description
q.save()
except Question.DoesNotExist:
Question.objects.filter(number = number).delete()
q = Question(number = number,question = description)
q.save()
if choice_formset.is_valid():
choice_formset.save()
else:
choice_formset = ChoiceQuestionFormSet(prefix='choice',)
uform = QuestionForm(initial={},prefix='question',)
return render_to_response('form.html',{'form': uform,
'formset':choice_formset})
#####
In these example instance=uform.instance is always null, even I just
save a model and make a mistake in some field. (submit is worked as
'save and conitue edit')
How can I recognize a instance object's pk for
"choice_formset = ChoiceQuestionFormSet(request.POST, request.FILES,
instance=uform.instance,prefix='choice',)"
igor.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---