第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Django - 跟蹤評論

Django - 跟蹤評論

qq_笑_17 2021-11-23 16:39:36
我正在構建一個網(wǎng)絡應用程序,其中每個產品都有自己的“配置文件”。我需要向模型添加某種字段,我可以在其中添加帶有日期和文本的“評論”,以跟蹤信息,例如公式更改、提供商更改、價格更改等。有任何想法嗎?models.py    from django.db import models# Create your models here.class Horse(models.Model):    name = models.CharField(max_length=255)    nacimiento = models.DateField(blank=True, null=True)    nro = models.IntegerField()    event = models.TextField()    slug = models.SlugField(unique=True)    def __str__(self):        return '%s-%s' % (self.name, self.nro)因此,對于發(fā)生的每個事件,我都需要一個帶有文本字段中提供的描述的新入口。
查看完整描述

2 回答

?
慕雪6442864

TA貢獻1812條經驗 獲得超5個贊

class HorseTracker(models.Model):

    horse = models.ForeignKey(Horse, on_delete=models.CASCADE, related_name='horse')

    comment = models.CharField(max_length=128)

    created_at = models.DateTimeField(auto_now_add=True)


    class Meta:

        ordering = ['-created_at']

每次更改模型中的某些內容時,您都可以創(chuàng)建新實例,HorseTracker并描述您所做的更改。


為了使它更有用TabularInline,您可以在您的HorseAdmin


class HorseTrackerInline(admin.TabularInline):

    model = HorseTracker


class HorseAdmin(admin.ModelAdmin):

    list_display = ['name', 'nacimiento', 'nro', 'event', 'slug', ]

    inlines = [ HorseTrackerInline, ]


查看完整回答
反對 回復 2021-11-23
?
紅糖糍粑

TA貢獻1815條經驗 獲得超6個贊

如果你想跟蹤各種模型,我建議使用類似django-simple-history 的東西來跟蹤模型中的變化。


將history字段添加到模型可讓您保存對字段所做的所有更改,然后訪問歷史記錄。如果要添加自定義消息,可以將字段添加到歷史模型,并在信號中設置消息。


from simple_history.models import HistoricalRecords


class MessageHistoricalModel(models.Model):

    """

    Abstract model for history models tracking custom message.

    """

    message = models.TextField(blank=True, null=True)


    class Meta:

        abstract = True


class Horse(models.Model):

    name = models.CharField(max_length=255)

    birthdate = models.DateField(blank=True, null=True)

    nro = models.IntegerField()

    event = models.TextField()

    slug = models.SlugField(unique=True)


    history = HistoricalRecords(bases=[MessageHistoricalModel,])

然后使用信號,您可以使用diff獲取更改,然后保存一條自定義消息,說明更改的是誰做出的。


from django.dispatch import receiver

from simple_history.signals import (post_create_historical_record)


@receiver(post_create_historical_record)

def post_create_historical_record_callback(sender, **kwargs):

    history_instance = kwargs['history_instance'] # the historical record created


    # <use diff to get the changed fields and create the message>


    history_instance.message = "your custom message"

    history_instance.save()

您可以生成一個非常通用的信號,適用于使用“歷史”字段跟蹤的所有模型。


注意:我將“nacimiento”重命名為“生日”,以保持用英語命名所有字段的一致性。


查看完整回答
反對 回復 2021-11-23
  • 2 回答
  • 0 關注
  • 349 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號