2017-01-01 78 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作爲反向關係。 –

相關問題