问题描述
我有一个理论模型不工作:
class Message(models.Model):
/ p>
text = models .TextField()
created = models.DateTimeField(auto_now_add = True)
sender = models.ForeignKey(User)
receiver = models.ForeignKey(User)
现在,如果一个用户对某人发送消息,比如私人消息线程,那么你有人发送了这个消息,而有人收到了这个消息,这是很重要的。这两个都应该是一对多我认为,因为每个用户可以发送无限的消息。发件人和接收者是来自同一模型用户的用户,但他们必须是不同的人。我得到django whining关于
django.core.management.base.CommandError:系统检查确定了一些问题:
/ p>
错误:
chat.Message.receiver:(fields.E304)Message.receiver的反向访问者与反向访问者冲突'Message.sender'。
提示:添加或更改相关名称参数到Message.receiver或Message.sender的定义。
chat.Message.sender:(fields.E304)Message.sender的反向访问者与Message.receiver的反向访问者冲突。
提示:添加或更改Message.sender或Message.receiver定义的related_name参数。
我应该添加unique = True,因为您不能向自己发送消息?这是在用户之间实现消息的不好的方法吗?我的错误是什么?谢谢,
Cody
对外键定义的参数:
class Message(models.Model):
text = models.TextField()
created = models.DateTimeField(auto_now_add = True)
sender = models.ForeignKey(User,related_name ='messages_sent')
receiver = models .ForeignKey(User,related_name ='messages_received')
这些相关名称可以在像这样:
user = User.objects.get(username ='johndoe')
messages_from = user.messages_sent.all()
messages_to = user .messages_received.all()
相当于:
user = User.objects.get(username ='johndoe')
messages_from = Messages.objects.filter(sender = user)
messages_to = Messages.objects.filter(receiver = user)
I have a theoretical model that isn't working:
class Message(models.Model): text = models.TextField() created = models.DateTimeField(auto_now_add=True) sender = models.ForeignKey(User) receiver = models.ForeignKey(User)
Now if a user messaged someone, like a private message thread, you have someone who sent it, and someone who received it, which is important to know. Both of these should be a one-to-many I think because each user can send infinite messages. Senders and receivers are users, from the same model User, but they must be different people. I get django whining about
django.core.management.base.CommandError: System check identified some issues:
ERRORS:chat.Message.receiver: (fields.E304) Reverse accessor for 'Message.receiver' clashes with reverse accessor for 'Message.sender'. HINT: Add or change a related_name argument to the definition for 'Message.receiver' or 'Message.sender'.chat.Message.sender: (fields.E304) Reverse accessor for 'Message.sender' clashes with reverse accessor for 'Message.receiver'. HINT: Add or change a related_name argument to the definition for 'Message.sender' or 'Message.receiver'.
Should I add unique=True since you can't send messages to yourself? Is this a bad way to implement messages between users? And what is my error? Thank you,
Cody
As the error message says add the related_name
argument to the foreign key definitions:
class Message(models.Model):
text = models.TextField()
created = models.DateTimeField(auto_now_add=True)
sender = models.ForeignKey(User, related_name='messages_sent')
receiver = models.ForeignKey(User, related_name='messages_received')
These related names can be used in the backward relations like this:
user = User.objects.get(username='johndoe')
messages_from = user.messages_sent.all()
messages_to = user.messages_received.all()
Which is the equivalent of:
user = User.objects.get(username='johndoe')
messages_from = Messages.objects.filter(sender=user)
messages_to = Messages.objects.filter(receiver=user)
这篇关于为什么在django模式中不能使用两个独立的外键来使用相同的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!