Struggling with One to One relationships

31 views
Skip to first unread message

Lachlan Musicman

unread,
Sep 16, 2014, 11:14:26 PM9/16/14
to django...@googlegroups.com
Hi

I have a super class Job that will never be instantiated on it's own,
although is not Meta.

There are two other models, WorkshopJob and EngineeringJob, which have
o2o links to a Job.

On creation of either WorkshopJob and EngineeringJob, a new Job is made.

The problem arises because any particular job may have both other
types linked to it - some Jobs will only be linked to either/or, but
some will have both.

Regardless of that, there will only ever be one WorkshopJob and or one
EngineeringJob per job.

I am struggling to create the second object - when I try to create a
new WorkshopJob from an existing EngineeringJob (ie, linking the
underlying Job id number to the as yet unsaved new Workshop job),
creation fails.

Is it because I have parent_link=True on both subclasses? Is it
because the o2o will only allow a single FK on an object? Or is it
possible - I've just made a mistake somewhere else (I tried CBVs, I
tried FBVs, nada).

Cheers
L.

--
You have to be really clever to come up with a genuinely dangerous
thought. I am disheartened that people can be clever enough to do that
and not clever enough to do the obvious thing and KEEP THEIR IDIOT
MOUTHS SHUT about it, because it is much more important to sound
intelligent when talking to your friends.
This post was STUPID.
-----------------------------------------------------------------------------------------------------------
The Most Terrifying Thought Experiment of All Time
http://www.slate.com/articles/technology/bitwise/2014/07/roko_s_basilisk_the_most_terrifying_thought_experiment_of_all_time.html

Collin Anderson

unread,
Sep 17, 2014, 11:13:29 AM9/17/14
to django...@googlegroups.com
what happens when you try?

Lachlan Musicman

unread,
Sep 17, 2014, 7:49:33 PM9/17/14
to django...@googlegroups.com
I get the same error across CBV and FBV when trying to create the
second o2o relationship to the super/concrete model Job

[2014-09-17 11:49:26] null value in column "date_opened" violates
not-null constraint

"date_opened" is a var on the Job model. I don't understand why it's
trying to create a new Job when I've already assigned one?

Details:

When I moved from CBV to FBV I explicitly created the secondary Job,
then assigned the o2o var to the job in question.

My urls are modelled on the job, to make passing the job easier. The
two "add" functions here are for adding the second o2o relationship:

url(r'^job/(?P<slug>[-\w]+)/workshop/$',
views.WorkshopJobDetail.as_view(), name='wsjob_detail'),
url(r'^job/(?P<slug>[-\w]+)/workshop/add/$', views.workshop_add, name='ws_add'),

url(r'^job/(?P<slug>[-\w]+)/engineering/$',
views.EngineeringJobDetail.as_view(), name='enjob_detail'),
url(r'^job/(?P<slug>[-\w]+)/engineering/add/$', views.engineering_add,
name='en_add'),

In the view:

def workshop_add(request, slug):
job = Job.objects.get(slug=slug)
if request.method == 'POST':
form = WSAddForm(request.POST)
if form.is_valid():
workshop_job = WorkshopJob()
workshop_job = job
workshop_job.quote_required = form.cleaned_data['quote_required']
workshop_job.invoice_number = form.cleaned_data['invoice_number']
workshop_job.part = form.cleaned_data['part']
workshop_job.serial_or_lot = form.cleaned_data['serial_or_lot']
workshop_job.serial_or_lot_number =
form.cleaned_data['serial_or_lot_number']
workshop_job.work_order = form.cleaned_data['work_order']
workshop_job.save()
return redirect(workshop_job)
else:
form = WSAddForm()
return render(request, 'jobs/workshop_form.html', {'form':form, 'object':job})






On 18 September 2014 01:13, Collin Anderson <cmawe...@gmail.com> wrote:
> what happens when you try?
>
> --
> 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...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/dea67a67-a3ee-4439-9a61-2f6f6cae98ef%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Lachlan Musicman

unread,
Sep 17, 2014, 7:50:37 PM9/17/14
to django...@googlegroups.com
When using the CBVs, the error actually made me consider that I should
be using UpdateView rather than CreateView, but that doesn't work - I
don't want to change the underlying Job, I just want to add another,
as yet non existent, o2o to it.

Cheers
L.

Collin Anderson

unread,
Sep 19, 2014, 6:43:09 PM9/19/14
to django...@googlegroups.com
Yes, this seems to be the usual problem with inheritance.

You need to do something scary like workshop_job.__dict__.update(job.__dict__) so that your workshop_job object will actually have all of the info from the job, like date_opened.

Lachlan Musicman

unread,
Sep 19, 2014, 9:24:14 PM9/19/14
to django...@googlegroups.com
Ah! OK, it's not just me then. Good.

It's a usual inheritance problem? I've never seen it with o2m or m2m
relations? I think I'll just change them to o2m and check to make sure
there's only one in the save method - seems clearer -easier to read
for !me, and less scary.

Thanks for your help

L.
> --
> 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...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/6fbb336e-bd13-4e94-bbfd-88c7bbe4be32%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages