atom feed3 messages in com.googlegroups.django-usersRe: Accessor clashes related field
FromSent OnAttachments
Carl KarstenJan 22, 2008 12:00 am 
Karen TraceyJan 22, 2008 7:21 am 
Carl KarstenJan 22, 2008 10:10 am 
Subject:Re: Accessor clashes related field
From:Carl Karsten (c.@personnelware.com)
Date:Jan 22, 2008 10:10:31 am
List:com.googlegroups.django-users

Karen Tracey wrote:

On Jan 22, 2008 3:00 AM, Carl Karsten <c.@personnelware.com> wrote:

# core/models.py from django.db import models from django.contrib.auth.models import User import ridgemoor.msg.models from time import strftime

class Message(models.Model): to = models.ForeignKey(User, related_name = "messages_received") sender = models.ForeignKey(User, related_name = "messages_sent") subject = models.CharField(max_length=50, blank=False) sent = models.DateTimeField(auto_now_add=True) recieved = models.DateTimeField(null=True) body = models.TextField() status = models.CharField(max_length=1, blank=True, null=True) def __str__(self): return self.subject class Admin: list_display = ('sent', 'to', 'sender', 'subject')

carl@asus17:~/ridgemoor$ PYTHONPATH=$HOME ./manage.py syncdb Error: One or more models did not validate: core.message: Accessor for field 'to' clashes with related field 'User.messages_received'. Add a related_name argument to the definition for 'to'.

Something broke my app. it is most likely the changes I have been making to get things working under apache, but this may be a change to trunk.

It is telling me to add something that is already there.

The error is telling you that by the time the "to" field of Message is processed, User already has a "messages_received" relation. Yes, the message is a bit misleading, since you already have a related_name field, but what it really means is you need to specify a related_name that doesn't already exist for User. What is in ridgemoor.msg.models? I'd guess there's another model with a ForeignKey to User specifying a related_name of "messages_received". Do you really need two models with this relation? If so you'll need to specify different related_names for the two.

Ah! bingo. fixed.

in answer to your question: # msg/model.py class Message(models.Model): to = models.ForeignKey(User, related_name = "messages_received") sender = models.ForeignKey(User, related_name = "messages_sent")

and for the last 6 months I had been using: class Message(msg.models.Model): status = models.CharField(max_length=1, blank=True, null=True)

and in at least one: # class Meta: # db_table = 'message'

I did this before I learned you shouldn't do it. It worked, might still work, but when I started bumping into problems I figured I better flatten it out. So I cut/pasted the attributes, changed the parent class to models.Model, and figured that would be all I needed. Didn't occur to me that relations create keys in other tables - now I see the copies are colliding.

Thanks for helping me.

Carl K