I do not guess I was clear with my problem, so I am going to try to
state it more clearly.
I know that I am defining two primary keys and this is the problem.
Using a OneToOneField as a primary key is fine. However, I want to
set up the base abstract model to *only* define the primary key (id =
SomeAwesomeIDField(primary_key=True)) if the inheriting class does not
define a primary key... similar to the way that the default model
does with an AutoField. I do not want subclasses of my base class to
use the auto increment primary key by default.
I used different names for the primary keys because I was trying to
show that I did not want to force the primary key to be named "id".
Rather, I want to check for any primary key already present on the
class. Hence the use of "a" and a OneToOneField.
For example, one solution to the problem would be to use two abstract
base classes. One that defines common fields, and one that defines
the id. And then only use both abstract classes for model A and only
common fields for model B.
However, I would prefer to only have one base class. As such, I am
looking to implement an automatic id field similar to how
django.db.models.Model does it. The problem is that after looking at
the source code, I am not sure where this takes place.
So, is it possible to conditionally define model fields in an abstract
model base class depending on the fields defined in the subclass?
If not, why? Where and/or how does this automatic field get assigned
when using models.Model?
Thanks
On Nov 5, 7:02 pm, Kurtis Mullins <
kurtis.mull...@gmail.com> wrote:
> Good point, Furbee!
>
> "g", I was going to suggest just overriding that "id" field -- but I read
> the docs real quick and they say:
>
> "It is an error to have fields in the abstract base class with the same
> name as those in the child (and Django will raise an exception)."
https://docs.djangoproject.com/en/dev/topics/db/models/#abstract-base...
>
> So yeah, the initial error is definitely that you're trying to have
> multiple primary keys for a single table (Model). In the end, you might
> also have problems trying to use a relationship as a Primary key as well.
>
>
>
>
>
>
>
> On Sat, Nov 5, 2011 at 6:53 PM, Furbee <
Furbeena...@gmail.com> wrote:
> > I can't speak to the reference field being used as a primary key, but the
> > initial error is that you have multiple primary keys on the single model B.
> > If it 'is-a' class Base, it inherits id as a primary key, and also defines
> > a and a primary key. You cannot have multiple primary key fieds, hence
> > "table B has more than one primary key."
>
> > Furbeenator
>
> > On Sat, Nov 5, 2011 at 3:48 PM, Kurtis Mullins <
kurtis.mull...@gmail.com>wrote:
>
> >> I don't think the problem is overriding the Primary Key. I could be
> >> wrong, but I think the actual problem here is that you can not use
> >> OneToOneFields, ManyToManyFields, or ForeignKeys as primary keys. I'm not
> >> sure how it would work on a database level. Like I said, I could be wrong
> >> -- but that's my initial hunch. Try overriding it with something else like
> >> an IntegerField and see if you still get an error.
>