2017-01-01 77 views
0

我使用Django的渠道,我想能够附加字段保存到数据库后的json数据一起。节省额外的数据,以Django的数据库

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

Postmie模型是一样的一个在Django的渠道教程Post发现here,唯一的区别是,我的模型在我的用户模型中的额外的外键指向电子邮件字段。

email=request.user.email不起作用。我正在考虑将电子邮件放在隐藏的字段中,但这对我来说似乎并不安全。

我正在使用的方法在Django频道教程中发现的方法几乎相同,这些教程发现在hereconsumers.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 
+0

你说的意思是什么“外键创建了一个名为EMAIL_ID场”。你能向我们展示Postmie的模型吗?你可以使用用户作为foriegn密钥吗? –

+0

我添加了Postmie模型。我创建的外键叫做'email'。但是在数据库中创建的列被称为'email_id'。与'feed'字段相同,数据库中的列名被称为'field_id'。我认为这是Django做事的方式。 – Jam1

+0

这里,问题是'email_id'是外键'email'对象的'id'。您可以创建一个'post_save'信号来创建电子邮件对象,并将该对象与它关联。 – karthikr

回答

0

假设Usermie是你的用户模型。这意味着你在settings.py

有AUTH_USER_MODEL =“yourapp.Usermie”如果你不使用to_field,你可以这样做,

我认为你需要做以下

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_field:只有在Django> 1.10

按照documentation,电子邮件应该是唯一的。

如果创建Postmie后to_field改变。请确保列中的所有值都具有新的对应值。

+0

这是行不通的。我认为这是因为'request.user.id'不会返回一个字符串,或者它没有给出可存储的值或者可以在这个“上下文”中的数据库中表示的值。在我的导航栏中有一个合适的“上下文”,我有'request.user.username',并且它在html页面中为用户提供了一个字符串,但是在Django的函数中它被解释为别的东西。这是我至少从中得到的。谢谢您的帮助! – Jam1

+0

想一想,'request.user.id'或'request.user.something'应该是所有上下文中的字符串。 – Jam1

+0

我认为您在settings.py中缺少AUTH_USER_MODEL ='yourapp.Usermie'。这应该使用您的模型作为AUTH_USER并将其附加到request.user。在所有情况下,id或pk总是整数。如果你在Usermie模型中有一对一的字段给用户。你可以用request.user.usermie作为反向关系。 –

相关问题