App_step.readme_id may not be NULL

20 views
Skip to first unread message

Wnt2bsleepin

unread,
Jan 30, 2014, 12:08:22 PM1/30/14
to django...@googlegroups.com
I am new to Django and am trying to save two objects from the same form. 

Models:

class ReadmeTemplate(models.Model):
    """
        Represents a bugfix README

        Contains step models, which contain multiple steps.
    """

    def __unicode__(self):
        return self.title

    title = models.CharField(max_length=200)
    author = models.CharField(max_length=200)
    user = models.ForeignKey(User,editable=False)


class Step(models.Model):
    """
        Represents a single step in a Readme Template
    """
    step_text = models.TextField()
    readme = models.ForeignKey(ReadmeTemplate)


I am able to save the ReadmeTemplate fine, but the Steps are not able to be saved. Here is what I am trying in the view.

def createTemplate(request):
    StepFormSet = modelformset_factory(Step, max_num=100, form=CreateStepForm,extra=1)
    if(request.method == 'POST'):
        readme_form = CreateReadmeTemplateForm(request.POST)
        step_forms = StepFormSet(request.POST)
        if readme_form.is_valid() and step_forms.is_valid():
            try:
                readme_object = readme_form.save()
                readme_object.user = request.user
                readme_object.save()
                step_forms.readme_id = readme_object.id
                step_forms.save()
            except forms.ValidationError, error:
                readme_object = None

            return HttpResponse('<script type="text/javascript">opener.dismissAddAnotherPopup(window, "%s", "%s");</script>' % (escape(readme_object._get_pk_val()), escape(readme_object)))
        return render(request,'App/newtemplate.html',{'readme_form':readme_form,'step_forms': step_forms})
    else:
        readme_form = CreateReadmeTemplateForm()
        step_forms = StepFormSet(queryset=Step.objects.none())
        return render(request,'App/newtemplate.html',{'readme_form':readme_form,'formset': step_forms})


Is there anyway to get the id of the readme object and assign it to the step object? Thanks for any help.

donarb

unread,
Jan 30, 2014, 12:29:42 PM1/30/14
to django...@googlegroups.com
All you need to do is to set the readme_object to the readme attribute of step_forms, Django will extract the id for you and set it appropriately.

step_forms.readme = readme_object


Wnt2bsleepin

unread,
Jan 30, 2014, 1:02:06 PM1/30/14
to django...@googlegroups.com
Didn't seem to work. The step_forms is a modelformset_factory, so I don't know if that makes a difference. I would also like to assign a user to the readme_form, but 

                
    readme_form.user_id = request.user.id
    readme_object = readme_form.save()
    step_forms.readme = readme_object
    step_forms.save() 

doesn't work. 

 

C. Kirby

unread,
Jan 30, 2014, 2:27:49 PM1/30/14
to django...@googlegroups.com
A model formset gives you multiple instances of the modelform, so you need to iterate through them:

                readme_object = readme_form.save()
                readme_object.user = request.user
                readme_object.save()
                for step_form in step_forms:
                    step_form.readme = readme_object
                    step_form.save()

To set the user is basically the same answer as donarb provided. Don't bother with going down to the id level when using a foreign key, just use the object.
So here just use:
   readme_form.user = request.user

Wnt2bsleepin

unread,
Jan 30, 2014, 5:27:08 PM1/30/14
to django...@googlegroups.com
Neither seem to work

    readme_form.user = request.user
    readme_object = readme_form.save()
    step_forms.readme_template = readme_object
    step_forms.save()

or

    readme_form.user = request.user
    readme_object = readme_form.save()
    step_forms.readme = readme_object
    step_forms.save()

It is still giving me the integrity error, and the readme model doesn't seem to have the user assigned to it.
Reply all
Reply to author
Forward
0 new messages