1

I have my models like this.

class Subscriber(models.Model):
    skillset = models.TextField(verbose_name='Skill Set')
    slug = models.SlugField(unique=True, default='')

class SoloDeveloper(models.Model):
    first_name = models.CharField(max_length=30, verbose_name='First Name')
    last_name = models.CharField(max_length=30, verbose_name='Last Name')
    subscriber = models.OneToOneField(Subscriber, related_name='solo_dev', on_delete=models.CASCADE)

I am trying to save the solo developer and assign it to the one to one field of the Subscriber.

def solo_dev_edit(request, slug):
    subscriber = Subscriber.objects.get(slug=slug)

    subscriber_form = SubscriberForm()
    solo_dev_form = SoloDevForm()

    if request.method == 'POST':
        subscriber_form = SubscriberForm(data=request.POST, instance=subscriber)
        solo_dev_form = SoloDevForm(data=request.POST)

        if all[subscriber_form.is_valid(), solo_dev_form.is_valid()]:
            sub = subscriber_form.save(commit=False)
            solo_dev = solo_dev_form.save()
            sub.solo_dev = solo_dev
            sub.save()
            return redirect('solo_dev_view', slug=subscriber.slug)

    else:
        subscriber_form = SubscriberForm()
        solo_dev_form = SoloDevForm()

    return render(request, 'main/solo_dev_edit.html', {
        'sub_fm': subscriber_form,
        'solo_dev_fm': solo_dev_form,
    })

This fails saying

null value in column "subscriber_id" violates not-null constraint
DETAIL: Failing row contains (9, john, doe, null).

What am I doing wrong?

Animesh
  • 4,926
  • 14
  • 68
  • 110

2 Answers2

2

Your subscriber field on SoloDeveloper does not allow null values. And you're trying to save the SoloDeveloper before the Subscriber -- meaning the subscribed_id (the id of the subscriber) will be None, causing the error. Try this:

        sub = subscriber_form.save()
        solo_dev = solo_dev_form.save(commit=False)
        solo_dev.subscriber = sub
        solo_dev.save()
CoffeeBasedLifeform
  • 2,296
  • 12
  • 27
  • I understand what you are saying, but in my case subscriber is the main user (subscriber) that gets created upon registration and solo dev is one-to-one to subscriber so that he can be identified as the solo dev who got the subscription. That is why in the solo dev edit view, I will already have the subscriber created. I am saving subscriber ahead because subscriber model also has some common data for any type of subscriber, like the skill set. Hope I was clear. – Animesh Feb 19 '18 at 18:54
  • Kind of a similar scenario as explained here. http://www.joshuakehn.com/2013/7/18/multiple-django-forms-in-one-form.html – Animesh Feb 19 '18 at 19:23
  • Kind of figured it out. Posted the answer. Hope this is not the wrong way. – Animesh Feb 19 '18 at 19:37
0

Figured that the subscriber is not being saved, so I put saving solo dev on hold until the subscriber is saved. This helped.

sub = subscriber_form.save(commit=False)
solo_dev = solo_dev_form.save(commit=False)
sub.solo_dev = solo_dev
sub.save()
solo_dev.save()
Animesh
  • 4,926
  • 14
  • 68
  • 110