вторник, 27 сентября 2011 г.

Django: "Accessor for field... clashes with related field" error after syncdb


If you see error message
Accessor for field ... clashes with related field
after syncdb command then likely problem in that one of your tables reffers another tables by 2 (or more) of its fields..

Example:


class User(models.Model):
     name = models.TextField()
     birth_date = models.DateTimeField()

class Message(models.Model):
     from = models.ForeignKey(User)
     to = models.ForeignKey(User)
     text = models.TextField()
#table 'Messages' has two links to table 'User'

Django could resolve this issue by defaulf by adding incremental postfix to accessor names for example.. But I have never hear about such django feature :)

To cut a long story short.. problem is solved by adding extra attributes for every referencing field:


class User(models.Model):
     name = models.TextField()
     birth_date = models.DateTimeField()

class Message(models.Model):
     from = models.ForeignKey(User,related_name="message_sender")
     to = models.ForeignKey(User,related_name="message_reciever")
     text = models.TextField()

syncdb shouldnt show accessor errors after that..

Комментариев нет:

Отправить комментарий

Если Вы нашли ошибку у автора, у Вас есть вопрос или просто хотите поделиться чем-то полезным, то пишите - не стесняйтесь..