Parent_link=True causes Cannot assign None: "Business.id" does not allow null values.

165 views
Skip to first unread message

phoebebright

unread,
Feb 11, 2009, 1:36:20 PM2/11/09
to Django users
I am following the instructions at the bottom of this page
http://docs.djangoproject.com/en/dev/topics/db/models/#inheritance-and-reverse-relations
and here http://docs.djangoproject.com/en/dev/ref/models/fields/#onetoonefield

I have a parent class Directory and a child classes Business, Tourism
and Community. The child classes have a subcategory that I want to be
able to access from a listing of Directory. My reading of the
documentation was that by defining a onetoone field with
parent_link=True I could do this, but on going into Admin and trying
to add a new entry to Business I immediately get the error Cannot
assign None: "Business.id" does not allow null values.

Where is my error (again......):

class Directory(models.Model):
name = models.CharField(max_length=60)
...

class Business(Directory):
id = models.OneToOneField(Directory, parent_link=True,
related_name="entries")
cat = models.ForeignKey(Subcategory, limit_choices_to =
{'category__exact': 2})


And the SQL table it created, which looks correct to me:

CREATE TABLE `town_business` (
`id_id` int(11) NOT NULL,
`cat_id` int(11) NOT NULL,
PRIMARY KEY (`id_id`),
KEY `town_business_cat_id` (`cat_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Or am I trying to be too clever all together? This is the query that
does everything except pull in the cat from the subclass.

entries = Directory.objects.filter(is_live=True).select_related
().distinct()




Karen Tracey

unread,
Feb 11, 2009, 2:30:55 PM2/11/09
to django...@googlegroups.com
On Wed, Feb 11, 2009 at 1:36 PM, phoebebright <phoebeb...@gmail.com> wrote:

I am following the instructions at the bottom of this page
http://docs.djangoproject.com/en/dev/topics/db/models/#inheritance-and-reverse-relations
and here http://docs.djangoproject.com/en/dev/ref/models/fields/#onetoonefield

I have a parent class Directory and a child classes Business, Tourism
and Community.  The child classes have a subcategory that I want to be
able to access from a listing of Directory.  My reading of the
documentation was that by defining a onetoone field with
parent_link=True I could do this, but on going into Admin and trying
to add a new entry to Business I immediately get the error Cannot
assign None: "Business.id" does not allow null values.

Where is my error (again......):

I cannot recreate any error using my own models based on the snippets you've provided, so it seems you've left out some key part of your config that is causing what you are seeing.  Perhaps post your models.py and admin.py files on someplace like dpaste?  Also please state exactly what level of Django you are using?

Karen

phoebebright

unread,
Feb 11, 2009, 3:19:27 PM2/11/09
to Django users
Karen,

Have done a stripped down version and still getting the error so put
the requested code here: http://dpaste.com/119483/ and http://dpaste.com/119486/.

using Django version 1.1 pre-alpha,

Thanks for your interest! Have to finish this tonight.

Phoebe.

On Feb 11, 7:30 pm, Karen Tracey <kmtra...@gmail.com> wrote:
> On Wed, Feb 11, 2009 at 1:36 PM, phoebebright <phoebebright...@gmail.com>wrote:
>
>
>
>
>
> > I am following the instructions at the bottom of this page
>
> >http://docs.djangoproject.com/en/dev/topics/db/models/#inheritance-an...

Karen Tracey

unread,
Feb 11, 2009, 3:57:12 PM2/11/09
to django...@googlegroups.com
On Wed, Feb 11, 2009 at 3:19 PM, phoebebright <phoebeb...@gmail.com> wrote:

Karen,

Have done a stripped down version and still getting the error so put
the requested code here: http://dpaste.com/119483/ and http://dpaste.com/119486/.

using Django version 1.1 pre-alpha,

Thanks for your interest!  Have to finish this tonight.

This is just a guess, since I don't really have time to look into this closely at the moment, but I'd try using something other than 'id' as the name of your OneToOne with parent_link field=True field.

Karen

phoebebright

unread,
Feb 11, 2009, 5:42:43 PM2/11/09
to Django users
Karen,

Spot on! Changed field name to directory and all is well.
Thanks so much.

Phoebe.


On Feb 11, 8:57 pm, Karen Tracey <kmtra...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages