1

I tried to create a model "Tag Container" that stores tags. Then i tried to connect such a container with OneToOneFields to the model where i need it:

from tagging.fields import TagFields

class TagContainer(models.Model):
  tags=TagFields

class UserProfile(models.Model):
  cont1=models.OneToOneField(TagContainer,related_name="cont1",null=True,blank=True)
  cont2=models.OneToOneField(TagContainer,related_name="cont2",null=True,blank=True)

After doing so i had some of the behaviour i wanted: When assigning a TagContainer to a models cont1 and trying to assign the same container to another models cont1 that didn't work. But when i try to assign the same TagContainer to cont1 and cont2 of the same model, that works. That same behaviour is occuring when i try to use ForeignKeys with unique=True.

I am not able to ensure that the fields in an instance of UserProfile point to two different instances of TagContainer. Is that possible?

edit:

Just found out that i can assign each TagContainer one time to cont1 and one time to cont2. It doesn't matter if it is of the same UserProfile.

marue
  • 5,588
  • 7
  • 37
  • 65
  • 1
    FWIW, the same behavior occurs with ForeignKey's with unique=True, because that's all a OneToOneField is. It's just a wrapper around ForeignKey with unique=True. – Chris Pratt Feb 26 '12 at 17:52
  • At least that cleared there really is no difference between the usage of OneToOne and FK, besides the code being more transparent. – marue Feb 26 '12 at 17:57

0 Answers0