跳转到内容
View in the app

A better way to browse. Learn more.

彼岸论坛

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.
欢迎抵达彼岸 彼岸花开 此处谁在 -彼岸论坛

[Python] 问一个 django 编辑信息扣除相应用户积分的问题

发表于

问一个 django 编辑信息扣除相应用户积分的问题

class EditTopicForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        
        self.user = kwargs.pop('user', None)
        self.topic_id = kwargs.pop('topic_id', None)
        #以上两条语句取不到相应的值
        super(EditTopicForm, self).__init__(*args, **kwargs)
        
    class Meta:
        model = Topic
        #fields = "__all__"
        fields = ['title', 'body', 'ys_t','is_cuti']
    def save(self, commit=True):
        inst = super(EditTopicForm, self).save()        
        if inst.is_cuti:
            inst.author.levels -= 2
            inst.author.save(update_fields = ['levels'])
        if inst.ys_t:
            inst.author.levels -= 2
            inst.author.save(update_fields = ['levels'])
        return inst

本意是用户发布信息后,编辑信息时如果给标题“加粗”,变“颜色”时就会扣除用户积分 2. 如果不做此操作的话就不扣除积分

问题是如果用户本身就已经“加粗”或变颜色了。那再编辑信息只改其它如标题或内容时,这样的情况就不再扣积分了。

如上面代码只要编辑信息就会扣除相应的积分。 我现在想到的办法 一. 把加粗或变色单独写一个操作函数。 二. 在 models 或 forms 里 在修改信息时 直接在 def save()里实现。就是先取到修改前信息的 is_cuti ys_t 字段的值跟修改后的值做比较后再决定是否执行扣积分问题

现在不知道怎么先取到修改前的值? 如上面所示代码都是取到的修改后的值。 在构造函数里 init 里取不到东西。也可能我写的代码不对

不知道哪位朋友有什么方法能实现这个功能吗? 多谢了

Featured Replies

No posts to show

创建帐户或登录来提出意见

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.