Django-如何映射用户之间发送的消息 [英] Django- How to map messages sent between users

查看:148
本文介绍了Django-如何映射用户之间发送的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为应用程式导入功能,让使用者能够传送活动邀请给其他使用者。邀请本身需要能够获得关于在事件处执行的组的信息,关于事件本身的信息(即日期/时间,地点,位置)以及关于发送者和接收者的信息。来自使用PHP的数据库工作,我会更加简单地认为创建一个数据库表的邀请,其中包含事件的id,组的id和用户id的发件人和收件人的字段,除了要与其一起发送的消息。这个结构将使得所有的邀请存储在同一个表中,并且每当用户想要检查他们的邀请时,应用将查询该表的所有具有他们的ID的行。

I'm implementing a feature for an app involving users being able to send event invites to other users. The invite itself needs to be able to get information about the group performing at the event, the info about the event itself (i.e. date/time, venue, location), and info about the sender and recipient. Coming from having worked with databases using PHP, I would more trivially think to create a database table for invite which has fields for the id of the event, the id of the group, and user ids for both the sender and recipient, in addition to the message to be sent with it. This structure would make it so that all invites are stored in the same table and whenever a user wants to check their invites, the app would query that table for all of the rows with their id.

问题是,对于一个,django使用模型(我真的不习惯使用),其中每次创建对象时隐式设置ids。除此之外,由于我使用内置模型为用户,django提供,我不真的确定如何在创建invites表时调用用户模型。另一个想法,我是扩展Django的用户模型添加许多字段,这将为收件人和发件人(两个类型用户)添加外键,这将是我所理解的后向相关对象。 Django文档建议反对这一点,所以我不认为这是最好的方式来构造invites表。

The problem is that for one, django uses models (which I'm not really used to using yet) where the ids are implicitly set every time an object is created. In addition to this, since I'm using the built in model for users, which django provides, I'm not really sure how to go about invoking the user model when creating the invites table. Another idea that I had was to extend Django's user model adding a many to many field which would add foreign keys for the recipient and the sender (both of type user) which would be backward related objects from what I understand. The Django docs advise against this however, so I don't think it would be the best way to structure the invites table.

我应该如何构建这个功能?

How should I go about structuring this feature? Any help would be appreciated.

下面是invites表的当前实现(返回相关name属性的名称错误):

Here is the current implementation I have for the invites table (which is returning a name error for the related name attribute):

 class User(AbstractBaseUser):
    email = models.EmailField()
    # full_name = models.CharField(max_length = 120, blank = True, null = True)
    user_name = models.CharField(max_length = 40, unique =True)
    USERNAME_FIELD = "user_name"
    REQUIRED_FIELDS = ['email']
    timestamp = models.DateTimeField(auto_now_add = True, auto_now = False)
    updated = models.DateTimeField(auto_now_add = False, auto_now = True)
    invites = models.ManyToManyField(
        'self', through = 'Invite', 
        symmetrical = False )

    def __unicode__(self):
        return self.full_name

    def get_full_name():
        return self.



 class Invite(models.Model):
    sender_id = models.ForeignKey(User, related_name= invite_sender, on_delete = models.CASCADE )
    recipient_id = models.ForeignKey(User, related_name= invite_recipient, on_delete = models.CASCADE)
    concert_id = models.AutoField()
    artist_id = models.AutoField()
    message = models.CharField(max_length = 120, blank = True, null = True)
    date_sent = models.DateTimeField(auto_now_add = True, auto_now = False)


推荐答案

related_name的问题是它应该是一个字符串,如下:

The problem with the related_name is that it should be a string, like so:

sender_id = models.ForeignKey(User, related_name= 'invites_sent', on_delete = models.CASCADE )
recipient_id = models.ForeignKey(User, related_name= 'invites_received', on_delete = models.CASCADE)

这解决了您获取用户邀请的问题,因为这是一个相关的名称对于。假设您有一个用户实例化为此用户,您可以通过执行以下操作获取他发送的所有邀请:

this solves your problem with getting the users invites, as that is what a related name is for. assuming you have an user instantiated as this user you can get all the invitations he sent by doing:

invites = thisuser.invites_sent.all()

或者他收到的邀请:

invites = thisuser.invites_received.all()

也许有一个目的,我不知道,因为我是相当新的所有这一切,但我没有看到在 concert_id artist_id 字段,因为它们都是auto_field的,并且总是具有相同的值,这也是 id 字段,如你所指出的,django自动创建(当你没有另一个字段设置为模型的主键)。

Maybe there's a purpose that I don't know about, since I'm fairly new to all this, but I don't see the point in the concert_id and the artist_id field, since they're both auto_field's and are always gonna have the same value, which is also gonna be the same value of the id field that, as you pointed out, django automatically creates (when you don't have another field set as the primary key for the model).

我会做的是以下。定义邀请如下:

What I'd do is the following. Define invite as such:

class Invite(models.Model):
    sender = models.ForeignKey(User, related_name= 'invites_sent', on_delete = models.CASCADE )
    recipient = models.ForeignKey(User, related_name= 'invites_received', on_delete = models.CASCADE)
    message = models.CharField(max_length = 120, blank = True, null = True)
    date_sent = models.DateTimeField(auto_now_add = True, auto_now = False)


$ b b

,那么您可以创建如下的新邀请:

then you could create new invites like this:

from appname.models import User, Invite

inviter = User.objects.get(id=57) #enter whatever id is the inviter's id
invitee = User.objects.get(id=42) #enter id of person that is being invited
new_invite = Invite(sender=inviter, recipient=invitee, message='Hello, you are invited')
new_invite.save()

并获得用户邀请,例如:

and get users invites like this:

from appname.models import User, Invite

thisuser = User.objects.get(id=42)
invites_sent_by_user = thisuser.invites_sent.all()
invites_received_by_user = thisuser.invites_received.all()
invites_received_after_march_seventh= thisuser.invites_received.filter(date_sent__gt='2015-03-07')

通过在外键声明中指定的related_name访问邀请,用户模型上的manytomanyfield没有任何用途。

Since you're able to access the invites through the related_name you specified in the foreign key declaration that manytomanyfield on the user model serves no purpose.

希望我覆盖了大多数的一切

Hope I covered most everything

(可能有拼写错误,因为我在这里输入所有代码)

(there might be typos since I typed all the code here)

这篇关于Django-如何映射用户之间发送的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆