On Tue, May 6, 2008 at 3:01 PM, Peter Bailey <[EMAIL PROTECTED]> wrote:

>
> Hey Karen. I used manage.py validate. My model validated before I
> added the abstract class and changed the class signatures. But I
> probably did something silly. Here is a chunk of the code:
>
> # will use this for subclassing into our item subtypes
> class AbstractType(models.Model):
>    name = models.CharField(max_length=100)
>    class Meta:
>        abstract = True
>
> class Item(models.Model):
>    """Item information for survey pages - pages will contain one or
> more of these"""
>    sub_item = models.ForeignKey(AbstractType)


This is the problem: you can't have a ForeignKey to an abstract model.  See
this thread:

http://groups.google.com/group/django-developers/browse_thread/thread/ba191252874570e4/9c6096d5e7059c2c

particularly Malcolm's reply.  As mentioned in that thread, Django's way to
have a ForeignKey "point" to an unknown (or incompletely specified) type is
something called generic relations.  Though it might seem (as suggested at
the beginning of that thread) that generic relations could be dropped in
favor of model inheritance, that's not actually the case (as Malcolm
describes).

Alternatively, perhaps your use case would be better served by non-abstract
inheritance?  Inheritance doc is here:

http://www.djangoproject.com/documentation/model-api/#model-inheritance

You probably want to give it a careful read and see which way best fits with
they way you are approaching your problem.

[snipped remainder of model definitions]



> Well there is some of the model code - thanks for taking a look - I
> think I need to spend a couple of weeks reading 24 hours a day to get
> up to speed on all this technology, But I have just escaped from dot
> niet and asp and sql server etc etc. Oh, what a feeling, what a rush!
>

You are rather jumping into the deep end by starting off with inheritance,
which only recently landed in the Django trunk.  So there are probably not
that many blog posts or mailing list threads yet with guidance, sample
scenarios, how to use the feature. etc.  But if you're comfortable with a
steep learning curve, in the end I expect you will figure it out.  Good
luck!

Karen


> Cheers
>
> On May 6, 2:34 pm, "Karen Tracey" <[EMAIL PROTECTED]> wrote:
> > On Tue, May 6, 2008 at 2:25 PM, Peter Bailey <[EMAIL PROTECTED]>
> wrote:
> >
> > > Hey alen. I have tried implementing this and it makes good sense as
> > > far as I can see, but when I validate the model I always get an
> > > AttributeError: 'NoneType' object has no attribute 'name'.
> >
> > How are you validating the model?
> >
> > I have tried removing name from the definition of the class - same
> >
> > > message (not sure where it is getting 'name' from since I deleted it.
> > > I also looked around and saw that some other have had similar
> > > problems, and it seemed to be a string definition problem. So I put
> > > back 'name' and changed it to an IntegerField - same unhappy result.
> >
> > > Do you have any notion of what the problem is? The code looks right to
> > > me.
> >
> > It sounds like you have some code somewhere that is expecting to always
> be
> > handed an instance of your model, and thus be able to access the field
> > 'name', but it is in fact sometimes getting handed None.  When handed
> None,
> > it still tries to access 'name', but since None has no attribute 'name',
> you
> > get the error.  If you post the code someone could probably help
> pinpoint
> > the error more specifically.
> >
> > Karen
> >
> >
> >
> > > Thanks,
> >
> > > Peter
> >
> > > On May 6, 1:33 pm, Peter Bailey <[EMAIL PROTECTED]> wrote:
> > > > Thanks very much for your solution and reply alen. I'm learning the
> > > > ins and outs of python and django at the same time - fun adventure -
> > > > so far python is blowing me away - I love it. Better than anything I
> > > > have used before - 3 assemblers,c, c++ java, vb, ruby, php, asp, etc
> > > > (guess I am dating myself lol - most people don't seem to even ever
> > > > have looked at assembler these days. I used to love it - 7 years of
> > > > that after university) Oh, and of course C was always the bomb too
> :-)
> >
> > > > Thanks again,
> >
> > > > Peter
> >
> > > > On May 6, 12:46 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]>
> > > > wrote:
> >
> > > > > Define a 'abstract' attribute of Meta inner class and set it to
> true
> > > > > like so:
> >
> > > > > class AbstractType(models.Model):
> > > > >     name = models.CharField(max_length=100)
> >
> > > > >     class Meta:
> > > > >         abstract = True
> >
> > > > > class RadioBoxTypes(AbstractType):
> > > > >     radio_lable = models.CharField(max_length=20)
> >
> > > > > Regards,
> > > > > -Alen
> >
> > > > > On May 6, 5:43 pm, Peter Bailey <[EMAIL PROTECTED]> wrote:
> >
> > > > > > I have designed a small db model (on paper) and want to
> implement it
> > > > > > in my models.py file. So far, this has been pretty straight
> forward,
> > > > > > but I have a generic superclass and several subclasses, and I am
> > > > > > unsure how to implement this.
> >
> > > > > > My DB has page objects (webpages) with a few common attributes,
> and
> > > a
> > > > > > fk to an Item object. The item object is the generic superclass.
> It
> > > > > > could be a RadioType, a CheckBoxType, a VerbatimType, etc. These
> all
> > > > > > have attributes specific to themselves.
> >
> > > > > > Anyway, I don't grok how to set up this type of relationship in
> my
> > > > > > models.py file. Is there a standard way of doing this, or does
> > > anyone
> > > > > > have an suggestions or can point me to some relevant info?
> >
> > > > > > e.g.     Pages - pointed to by fk in Items
> > > > > >           ----------
> >
> > > > > >           Items - has key to one of the below
> > > > > >           --------
> >
> > > > > > RadioBoxTypes   CheckboxTypes   VerbatimTypes   etc
> > > > > > -----------------------   -----------------------
> > > > > > ---------------------    ----
> >
> > > > > > Thanks very much. Sorry if this is a dumb question - always fun
> > > being
> > > > > > a newbie :-(
> >
> > > > > > Peter
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to