I'm wondering what the correct way to create a model instance with a django model form is when it contains a required FK? I think it may have to do with the exclude class property, but in the view I am trying override this before save.
Model:
class Profile(models.Model):
client = models.OneToOneField('auth.User')
first_name = models.TextField(blank=True,)
...
Form:
class ProfileForm(floppyforms.ModelForm):
class Meta:
exclude = ('client',)
model = Profile
widgets = {
'first_name': floppyforms.TextInput(attrs={'placeholder': 'First Name'}),
...
View:
def post(self, request):
form = ProfileForm(request.POST)
if form.is_valid():
form.save(commit=False)
form.client = User.objects.create(username=request.POST['email'],)
form.save()
return redirect('/success')
return redirect('/error')
The error:
django.db.models.fields.related.RelatedObjectDoesNotExist: Profile has no client.
Looking at the Admin, I can see that the user has been created however.

Cheers