将其他数据保存到Django数据库

将其他数据保存到Django数据库,django,django-channels,Django,Django Channels,我正在使用Django频道,我希望能够将附加字段与帖子的json数据一起保存到数据库中 我的Postmie模型中有一个外键,指向我的用户模型中的email字段Postmie是负责将帖子保存到数据库的模型。外键创建一个名为email\u id的字段。当我将帖子保存到数据库时,我还想抓取发表帖子的用户的电子邮件,并将其保存到数据库中。我该怎么做呢?我没有使用Django表单 我的Postmie模型与Django频道教程Post中找到的模型相同,唯一的区别是我的模型有一个额外的外键,指向我的用户模型中

我正在使用Django频道,我希望能够将附加字段与帖子的
json
数据一起保存到数据库中

我的
Postmie
模型中有一个外键,指向我的用户模型中的
email
字段
Postmie
是负责将帖子保存到数据库的模型。外键创建一个名为
email\u id
的字段。当我将帖子保存到数据库时,我还想抓取发表帖子的用户的电子邮件,并将其保存到数据库中。我该怎么做呢?我没有使用Django表单

我的
Postmie
模型与Django频道教程
Post
中找到的模型相同,唯一的区别是我的模型有一个额外的外键,指向我的用户模型中的电子邮件字段

email=request.user.email
不起作用。我想把电子邮件放在一个隐藏的字段中,但这对我来说并不安全

我使用的方法实际上与Django Channel教程中找到的
consumers.py
中的方法相同。一切正常,但我无法在数据库中输入其他字段以获取帖子

def save_post(message, slug, request):
    """
    Saves vew post to the database.
    """
    post = json.loads(message['text'])['post']
    email = request.user.email
    feed = Feed.objects.get(slug=slug)
    Postmie.objects.create(feed=feed, body=post email_id=email)
邮递模型:

@python_2_unicode_compatible
class Postmie(models.Model):
    # Link back to the main blog.
    feed = models.ForeignKey(Feed, related_name="postmie")
    email = models.ForeignKey(Usermie,
                              to_field="email",
                              related_name="postmie_email",  max_length=50)
    subject = models.CharField(max_length=50)
    classs = models.CharField(max_length=50, null=True, blank=True)
    subclass = models.CharField(max_length=50, null=True, blank=True)
    title = models.CharField(max_length=60, null=True, blank=True)
    body = models.TextField()
    date_created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    def __str__(self):
        return "#%i: %s" % (self.id, self.body_intro())

    def post_email(self):
        return self.email

    def post_subject(self):
        return self.subject

    def post_datetime(self):
        return self.datetime

    def get_absolute_url(self):
        """
        Returns the URL to view the liveblog.
        """
        return "/feed/%s/" % self.slug

    def body_intro(self):
        """
        Short first part of the body to show in the admin or other compressed
        views to give you some idea of what this is.
        """
        return self.body[:50]

    def html_body(self):
        """
        Returns the rendered HTML body to show to browsers.
        You could change this method to instead render using RST/Markdown,
        or make it pass through HTML directly (but marked safe).
        """
        return linebreaks_filter(self.body)

    def send_notification(self):
        """
        Sends a notification to everyone in our Liveblog's group with our
        content.
        """
        # Make the payload of the notification. We'll JSONify this, so it has
        # to be simple types, which is why we handle the datetime here.
        notification = {
            "id": self.id,
            "html": self.html_body(),
            "date_created": self.date_created.strftime("%a %d %b %Y %H:%M"),
        }
        # Encode and send that message to the whole channels Group for our
        # feed. Note how you can send to a channel or Group from any part
        # of Django, not just inside a consumer.
        Group(self.feed.group_name).send({
            # WebSocket text frame, with JSON content
            "text": json.dumps(notification),
        })

    def save(self, *args, **kwargs):
        """
        Hooking send_notification into the save of the object as I'm not
        the biggest fan of signals.
        """
        result = super(Postmie, self).save(*args, **kwargs)
        self.send_notification()
        return result

假设Usermie是您的用户模型。这意味着您在settings.py中有AUTH_USER_MODEL='yourapp.Usermie'

如果您不使用to_字段,您可以执行,

我认为你需要做以下几点

Postmie.objects.create(feed=feed, body=post email=request.user)
或者你可以

Postmie.objects.create(feed=feed, body=post email_id=request.user.id)
您应该知道,每个外键通常都在数据库中表示,字段名后面加上_id。Django就是这样放置外键的。通常您应该直接使用Django的ORM

如果您正在使用to_字段:仅在Django>1.10中使用

根据标准,电子邮件应该是唯一的


如果在创建邮递后更改了“收件人”字段。请确保列中的所有值都具有新的对应值。

外键创建一个名为email\u id的字段是什么意思。你能给我们看看邮递员的模型吗?你能使用用户作为外键吗?我添加了
Postmie
模型。我创建的外键称为
email
。但在数据库中创建的列称为
email\u id
。与
feed
字段相同,数据库中的列名称为
field\u id
。我认为这是Django做事的方式。这里的问题是
email\u id
是foreignkey
email
对象的
id
。您可以创建一个
post_save
信号来创建电子邮件对象,并将此对象与之关联。是的,它是父表的id,但是我使用
to_字段
将其更改为实际电子邮件。谢谢@karthikr我会用
post\u save
试试你说的话,但这不起作用。我认为这是因为
request.user.id
没有返回字符串,或者它没有提供一个可存储的值,或者不能在这个“上下文”中在数据库中表示。一个合适的“上下文”就像在我的导航栏中,我有
request.user.username
,它在html页面中将用户名作为字符串提供给您,但在Django的函数中,它被解释为其他内容。这至少是我从中得到的。谢谢你的帮助!想想看,
request.user.id
request.user.something
在所有上下文中都应该是字符串。我认为您在settings.py中缺少AUTH\u user\u MODEL='yourapp.Usermie'。这应该使用您的模型作为AUTH_USER,并将其附加到request.USER。在所有情况下,id或pk始终是整数。如果在Usermie模型中有一对一字段对用户。您可以将user request.user.usermie作为反向关系。实际上,我在我的
settings.py
中有我的
AUTH\u user\u MODEL='usermie.usermie'
,并且所有内容都已注册
admin.py
。我可以在html页面
{{request.user.email}}
上执行此操作,并将其作为字符串呈现在html页面上。但由于某些原因,将其放入数据库是不起作用的。为什么要使用此字段?你能检查一下,如果你用“email=request.user.email”来代替吗。