Django model problem

4 views
Skip to first unread message

Albert

unread,
Apr 3, 2009, 4:14:13 PM4/3/09
to Django users
Hi,

Say I have these simple models:

class Musician(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)

class Album(models.Model):
artist = models.ForeignKey(Musician)
name = models.CharField(max_length=100)


Now in `Musician` I want to add a field "last_album".
How can I do that? I'd like to be able to write:

m = Musician(pk=1)
print m.last_album.name

When I try:
last_album = models.OneToOneField(Album)

it somehow works but creates a (useless) unique index for last_album.
And I don't need additional related field in the Album model (it's
also
created now).


Thanks for help!

Briel

unread,
Apr 3, 2009, 5:08:44 PM4/3/09
to Django users
Hi.
A quick solution would be to add a datefield to the album
the release date. You could then use that to find the
latest and it would be updated automatically when
adding new albums. You could use ot for other things
as well, but I don't know your needs. Will also make the
lookup a bit slower but shouldn't be that bad.

~Jakob

Marco Buttu

unread,
Apr 3, 2009, 5:26:00 PM4/3/09
to django...@googlegroups.com
On Fri, 2009-04-03 at 13:14 -0700, Albert wrote:
>
> class Musician(models.Model):
> first_name = models.CharField(max_length=50)
> last_name = models.CharField(max_length=50)
>
> class Album(models.Model):
> artist = models.ForeignKey(Musician)
> name = models.CharField(max_length=100)
>
>
> Now in `Musician` I want to add a field "last_album".
> How can I do that? I'd like to be able to write:
>
> m = Musician(pk=1)
> print m.last_album.name

I suggest you this solution:

class Musician(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)

albums = models.ManyToManyField("Album")

def last_album(self):
return self.albums.order_by("pub_date")[-1]

class Album(models.Model):
artist = models.ForeignKey(Musician)
name = models.CharField(max_length=100)

pub_date = models.DateField()

--
Marco Buttu

MS

unread,
Apr 4, 2009, 4:25:26 AM4/4/09
to Django users
On Apr 3, 11:08 pm, Briel <toppe...@gmail.com> wrote:
> Hi.
> A quick solution would be to add a datefield to the album
> the release date. You could then use that to find the
> latest and it would be updated automatically when
> adding new albums. You could use ot for other things
> as well, but I don't know your needs. Will also make the
> lookup a bit slower but shouldn't be that bad.

Hi,

Well, right - I oversimplified my sample model. So the pub_date in
Album model would probably be useful
(pub_date = models.DateField()).

But this is not the issue. Say I don't want access to the *last* album
but to some arbitrary one.
For example the favorite album. In the database it should be just an
id of musician's favorite album.
No extra database indexes are necessary.

A non-django solution would be something like:

class Musician(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
favorite_album = models.IntegerField
(db_column='favorite_album_id')

I would somehow update the favorite_album_id field with the id of the
favorite album. That's not the problem.
But this solution has a drawback - the favorite_album is just an
integer. So I can't use the Django's
built-in ORM for it. I would like to be able to write (in my program):

m = Musician(pk=1)
print m.favorite_album.name

Or:

m = Musician(pk=1)
a = Album(id=7)
m.favorite_album = a

Is that possible?

It works when I write this:

class Musician(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
last_album = models.OneToOneField(Album,
db_column='last_album_id')

But when Django creates SQLs (via syncdb) it adds a database index for
last_album_id which is completely useless.

So maybe the question should be - can Django create a OneToOneField
without generating a
database index for it?

Regards,
MS
Reply all
Reply to author
Forward
0 new messages