django related_name for field clashes.

Posted by Absolute0 on Stack Overflow See other posts from Stack Overflow or by Absolute0
Published on 2011-01-11T14:39:35Z Indexed on 2011/01/11 14:53 UTC
Read the original article Hit count: 230

I am getting a field clash in my models:

class Visit(models.Model):
     user = models.ForeignKey(User)
     visitor = models.ForeignKey(User)

Error: One or more models did not validate:
profiles.visit: Accessor for field 'user' clashes with related field 'User.visit_set'. Add a related_name argument to the definition for 'user'.
profiles.visit: Accessor for field 'visitor' clashes with related field 'User.visit_set'. Add a related_name argument to the definition for 'visitor'.

what would be a sensible 'related_field' to use on visitor field? This model basically represents the visits that take place to a particular user's profile.

Also should I replace any of the ForeignKey's with a ManyToManyField? The logic is a bit confusing.

Edit: This seems to fix it, but I am unsure if its what I want. :)

 class Visit(models.Model):
      user = models.ForeignKey(User)
      visitor = models.ForeignKey(User, related_name='visitors')

© Stack Overflow or respective owner

Related posts about database

Related posts about django