No. One primary key per model.
> In fact I want that the id of an instance of this class is the
> concatenation of the (attribute 1 +attribute2).
> How to do this?
You might be able to do it with a custom field type and fair bit of
code. However, this is definitely not the approach to take if you are
just starting out.
Instead, just let the primary key be the default one Django provides you
with. You can have another property or method on the model that creates
your combined value that you can use for other purposes. Trying to
overly control the primary key almost always is not necessary.
What is the problem you are really trying to solve here?
Regards,
Malcolm
I'm miles behind on my django-users reading, but saw this floating
past... This isn't necessarily the happy answer, but I think table
adjustments might be the pragmatic approach here unless you have many
days to spend on the problem.
"Faking it" for a primary key column is hard. The reason being that
there is a lot of code internally which access SomeModel._meta.pk and
forces that into the column list or expects it to be present when
processing results.
Now, I said "hard" and not "impossible", because it might be possible to
make this work in a reasonably transparent fashion if you created a
custom field that was the concatenation of two other fields. After all,
that's kind of how GenericRelation fields work. I'd be focusing on how
model instances were created (QuerySet.iterator and get_cached_row() in
django.db.models.query) when testing this out if you were really
motivated to do so.
Multi-column primary keys is something that's been on my list for ages
and the reason it's not done yet is because it's just very fiddly
internally -- so many places to test and modify and assumptions to
identify.
Naturally you could solve your problem "properly" by making the
necessary changes to Django itself and using a modified version, but,
that's why I wrote the previous paragraph: it's certainly possible and
not necessarily that hard (easier than trying to write a fake field that
works with out-of-the-box Django, I would guess), but it would be fairly
fiddly and time consuming.
Regards,
Malcolm