Hi,

I'm pretty new to Django so feel free to laugh if something's
horrendously wrong here that I haven't spotted.

I'm trying to use a field from a foreign key as a primary key in
another model, but have no idea how to do this.
The idea is to have a table called Tag (columns called tagname and
tagversion, which create a composite primary key using the
unique_together option in the metadata).

The slightly odd constraint I have is that there can be multiple
versions of the same tag name, but only one of those of the same name
can be the "current" one. To do this I created another table called
CurrentTag to hold all of the current tag names in (I was going to do
this with a ForeignKeyField or OneToOneField but I'm not sure which is
more appropriate?).

The primary key for CurrentTag would be the tagname (as there should
only be one tag with that name in the CurrentTag table) and another
field/some sort of pointer for storing the "current" version number.

I changed the __unicode__ method to return tagname + tagversion in
combination so it is possible to differentiate them in the admin when
looking through them as a list (e.g. abc1, abc2 etc. where tagname =
abc and tagversion = 1 or 2 in this example), which is fine, until you
get to adding rows to CurrentTag...
At this point, the primary key of CurrentTag should be the tagname
field of the Tag table (as there should only be one current version
for a given tagname), however because I changed the unicode method to
return tagname + tagversion they show as separate items to add to the
CurrentTag table. That by itself is fine, but when a second row is
added with the same tagname it doesn't think they are teh same because
it doesn't use the tagname as the primary key.
e.g. I add abc1 to CurrentTag from a list showing the entries of the
Tag table. I then attempt to add abc2 and this succeeds, however this
not what I need, as it should say something like, "Current abc tag is
version 1, do you want to update it to 2?" instead.
Does this need to be done using a new set of custom forms and views,
or can I do this using Django's standard Field methods/options etc. in
the normal admin?

Here's the model code I've written:

class Tag(models.Model):
    tagname = models.CharField(max_length=100)
    tagversion = models.IntegerField()

    def __unicode__(self):
        return u'%s%s' % (self.tagname, self.tagversion)

    class Meta:
        unique_together = (("tagname", "tagversion"),)


class CurrentTag(models.Model):
    tag = models.OneToOneField(Tag)

    def __unicode__(self):
        return self.tag


Should I be using OneToOne field or ForeignKey or neither and some
other mechanism?

Apologies it's a bit of a long-winded question!

Thanks

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

Reply via email to