我使用Django的渠道,我想能夠附加字段保存到數據庫後的json
數據一起。節省額外的數據,以Django的數據庫
我在我的Postmie
模型中有一個外鍵,指向我的用戶模型中的email
字段。 Postmie
是負責將帖子保存到數據庫的模型。外鍵創建一個名爲email_id
的字段。在我將數據保存到數據庫的同時,我還想獲取發佈的用戶的電子郵件,並將其保存在數據庫中。我怎麼能這樣做呢?我沒有使用Django表單。
我Postmie
模型是一樣的一個在Django的渠道教程Post
發現here,唯一的區別是,我的模型在我的用戶模型中的額外的外鍵指向電子郵件字段。
email=request.user.email
不起作用。我正在考慮將電子郵件放在隱藏的字段中,但這對我來說似乎並不安全。
我正在使用的方法在Django頻道教程中發現的方法幾乎相同,這些教程發現在here和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)
Postmie型號:
@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
你說的意思是什麼「外鍵創建了一個名爲EMAIL_ID場」。你能向我們展示Postmie的模型嗎?你可以使用用戶作爲foriegn密鑰嗎? –
我添加了Postmie模型。我創建的外鍵叫做'email'。但是在數據庫中創建的列被稱爲'email_id'。與'feed'字段相同,數據庫中的列名被稱爲'field_id'。我認爲這是Django做事的方式。 – Jam1
這裏,問題是'email_id'是外鍵'email'對象的'id'。您可以創建一個'post_save'信號來創建電子郵件對象,並將該對象與它關聯。 – karthikr