AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-13135901

Viktor's questions

Martin Hope
Viktor
Asked: 2025-03-06 05:45:37 +0800 CST

Django 在 HTMX 响应中返回旧表单数据

  • 5

我进行了更改UpdateView,以便它也可以处理创建请求,如果pk=0:

class ObjectView(UpdateView):

    def get_object(self, queryset=None):
        if self.kwargs.get(self.pk_url_kwarg) == 0:
            return None
        return super().get_object(queryset)

    def form_valid(self, form):
        if self.request.htmx:
            return self.render_to_response(self.get_context_data(form=form))
        return redirect(self.object.get_absolute_url())

我View正在检查字段是否price已更新,如果是,price_date也更新该字段:

class PartView(ObjectView):
    model = Part
    form_class = PartForm

    def form_valid(self, form):
        if self.object:
            old = Part.objects.get(id=self.object.id)
            self.object = form.save(commit=False)
            if self.object.price != old.price:
                self.object.price_date = date.today()
            self.object.save()
        else:
            self.object = form.save()
        return super().form_valid(form)

我htmx从我的模板调用它:

<div hx-target="this">
  <form method="post"
        hx-post="{{ hx_url }}"
        hx-on:htmx:validation:failed="this.reportValidity()"
        hx-trigger="click from:#btn-save">
    {% csrf_token %}
    {{ form.price }}
    {{ form.price_date }}
  </form>  
  {% include "btn_save.html" %}
</div>

问题是,即使它成功更新了price_date数据库中的内容,它仍然会返回具有旧值的表单,现在如果我再次按“保存”,它将覆盖之前的更改。为什么会发生这种情况?

django
  • 1 个回答
  • 54 Views
Martin Hope
Viktor
Asked: 2024-12-10 09:31:59 +0800 CST

更快的词汇表生成

  • 7

我正在尝试在 Django 中创建一个目录,queryset如下所示:

def get_toc(self):
    toc = {}
    qs = self.get_queryset()
    idx = set()
    for q in qs:
        idx.add(q.title[0])
    idx = list(idx)
    idx.sort()
    for i in idx:
        toc[i] = []
        for q in qs:
            if q.title[0] == i:
                toc[i].append(q)
    return toc

但是它有时间复杂度O(n^2)。有没有更好的方法呢?

更新 我的意思是词汇表,而不是目录。

python
  • 3 个回答
  • 55 Views
Martin Hope
Viktor
Asked: 2024-08-22 04:20:19 +0800 CST

在 Django 中从数据库更新对象属性

  • 7

假设我有一个代表一项任务的模型:

class Task(models.Model):
    on_status = models.BooleanField()

    def run(self):
        if self.on_status:
            # do stuff

我使用 Celery Beat 运行此任务,并且在 Gunicorn 上运行仪表板,两者都使用相同的数据库。

app = Celery("app")

app.conf.beat_schedule = {
    "run_task": {
        "task": "run_tasks",
        "schedule": 5.0,
        "options": {
            "expires": 6.0,
        },
    },
}


tasks = Task.objects.all()


@app.task
def run_tasks():
    for task in tasks:
        task.run()

我可以on_status从仪表板进行更改,但随后我需要更新self.on_statusCelery Beat 进程内的实例。是否有命令可以从数据库更新属性值,或者是否有其他方法?

python
  • 1 个回答
  • 23 Views
Martin Hope
Viktor
Asked: 2024-02-20 05:57:11 +0800 CST

类型错误:抽象模型无法实例化(Django)

  • 6

我有抽象模型A和B一个子模型C。

class A(models.Model):
    field1 = models.CharField(max_length=24)

    class Meta:
        abstract = True


class B(models.Model):
    field1 = models.CharField(max_length=24)

    class Meta:
        abstract = True

   def make_A(self):
      A(field1=self.field1).save()


class C(B):
    pass

如果我尝试运行C.make_A()我会收到TypeError: Abstract models cannot be instantiated错误。所以我被迫重新编写make_A孩子的方法。A有没有办法编写在抽象下实例化的方法B?

python
  • 1 个回答
  • 19 Views
Martin Hope
Viktor
Asked: 2023-12-12 06:55:13 +0800 CST

Pandas 根据具有选定索引和 shift() 的切片更改 pandas 数据框中的值

  • 3

假设我有一个数据框df。首先我对其进行过滤,然后我需要找到满足条件的所有行,然后在原始数据帧的特定 bool 列中标记所有这些行以及每行的前 2 行:

df = pd.DataFrame({{'A': {0: nan, 1: nan, 2: 1944.09, 3: nan, 4: nan, 5: 1926.0, 6: nan, 7: 1930.31, 8: nan, 9: nan, 10: nan, 11: nan, 12: nan, 13: nan, 14: nan, 15: 1917.66, 16: 1920.43, 17: nan, 18: 1909.04, 19: nan, 20: nan, 21: nan, 22: nan, 23: nan, 24: 1920.05, 25: nan, 26: 1915.4, 27: 1921.87, 28: nan, 29: nan, 30: nan, 31: 1912.42, 32: 1920.08, 33: 1915.8, 34: nan, 35: nan, 36: nan, 37: nan, 38: 1919.71, 39: 1916.2, 40: nan, 41: 1926.79, 42: nan, 43: 1918.66, 44: nan, 45: 1925.5, 46: 1922.22, 47: nan, 48: nan, 49: 1927.87, 50: 1923.24, 51: nan, 52: 1929.53, 53: nan, 54: nan, 55: nan, 56: nan, 57: nan, 58: nan, 59: nan, 60: nan, 61: 1918.37, 62: nan, 63: nan, 64: 1923.61, 65: nan, 66: 1917.1, 67: nan, 68: nan, 69: nan, 70: nan, 71: nan, 72: nan, 73: nan, 74: nan, 75: nan, 76: nan, 77: nan, 78: nan, 79: nan, 80: nan, 81: 1924.48, 82: nan, 83: nan, 84: 1923.03, 85: nan, 86: nan, 87: nan, 88: nan, 89: 1926.87, 90: nan, 91: nan, 92: nan, 93: 1921.79, 94: nan, 95: 1925.27, 96: nan, 97: 1919.0, 98: nan, 99: nan, 100: 1923.74, 101: nan, 102: nan, 103: nan, 104: nan, 105: 1911.61, 106: nan, 107: 1923.33, 108: nan, 109: nan, 110: nan, 111: 1912.0, 112: nan, 113: 1915.8, 114: nan, 115: 1913.05, 116: nan, 117: nan, 118: nan, 119: nan, 120: nan, 121: nan, 122: 1916.93, 123: nan, 124: 1913.69, 125: nan, 126: nan, 127: nan, 128: nan, 129: 1918.38, 130: 1913.7, 131: nan, 132: nan, 133: nan, 134: nan, 135: nan, 136: 1919.5, 137: nan, 138: 1916.14, 139: nan, 140: nan, 141: nan, 142: nan, 143: nan, 144: 1921.28, 145: nan, 146: nan, 147: nan, 148: nan, 149: nan, 150: 1915.0, 151: nan, 152: nan, 153: nan, 154: nan, 155: nan, 156: 1927.48, 157: 1889.17, 158: nan, 159: 1921.91, 160: 1917.67, 161: 1923.23, 162: nan, 163: nan, 164: nan, 165: 1909.88, 166: nan, 167: 1913.82, 168: 1902.51, 169: nan, 170: nan, 171: nan, 172: nan, 173: nan, 174: nan, 175: nan, 176: nan, 177: nan, 178: nan, 179: 1920.15}, 'C': {0: False, 1: False, 2: True, 3: False, 4: False, 5: False, 6: False, 7: False, 8: False, 9: False, 10: False, 11: False, 12: False, 13: False, 14: False, 15: False, 16: False, 17: False, 18: False, 19: False, 20: False, 21: False, 22: False, 23: False, 24: False, 25: False, 26: False, 27: True, 28: False, 29: False, 30: False, 31: False, 32: True, 33: False, 34: False, 35: False, 36: False, 37: False, 38: False, 39: False, 40: False, 41: True, 42: False, 43: False, 44: False, 45: False, 46: False, 47: False, 48: False, 49: False, 50: False, 51: False, 52: True, 53: False, 54: False, 55: False, 56: False, 57: False, 58: False, 59: False, 60: False, 61: False, 62: False, 63: False, 64: False, 65: False, 66: False, 67: False, 68: False, 69: False, 70: False, 71: False, 72: False, 73: False, 74: False, 75: False, 76: False, 77: False, 78: False, 79: False, 80: False, 81: False, 82: False, 83: False, 84: False, 85: False, 86: False, 87: False, 88: False, 89: True, 90: False, 91: False, 92: False, 93: False, 94: False, 95: False, 96: False, 97: False, 98: False, 99: False, 100: False, 101: False, 102: False, 103: False, 104: False, 105: False, 106: False, 107: True, 108: False, 109: False, 110: False, 111: False, 112: False, 113: False, 114: False, 115: False, 116: False, 117: False, 118: False, 119: False, 120: False, 121: False, 122: False, 123: False, 124: False, 125: False, 126: False, 127: False, 128: False, 129: False, 130: False, 131: False, 132: False, 133: False, 134: False, 135: False, 136: False, 137: False, 138: False, 139: False, 140: False, 141: False, 142: False, 143: False, 144: False, 145: False, 146: False, 147: False, 148: False, 149: False, 150: False, 151: False, 152: False, 153: False, 154: False, 155: False, 156: True, 157: False, 158: False, 159: False, 160: False, 161: True, 162: False, 163: False, 164: False, 165: False, 166: False, 167: False, 168: False, 169: False, 170: False, 171: False, 172: False, 173: False, 174: False, 175: False, 176: False, 177: False, 178: False, 179: False}})

df2 = df[df.C]
print(df2)

2      1944.09
27     1921.87
32     1920.08
41     1926.79
52     1929.53
89     1926.87
107    1923.33
156    1927.48
161    1923.23
Name: A, dtype: float64

df3 = df2[(df2.A > df2.A.shift(1)) & (df2.A.shift(1) > df2.A.shift(2))]
print(df3)

52    1929.53
Name: A, dtype: float64

df[FROM_2nd_PREVIOUS_TO_EVERY_ROW_OF_df3_IN_df2:TO_EVERY_ROW_IN_df3, 'B'] = True
print(df)
           A        B
0        NaN    False
..       ...      ...
31   1234.56    False
32   1920.08    True
33   1234.56    True
..       ...      ...
41   1926.79    True
40   1234.56    True
..       ...      ...
51   1234.56    True
52   1929.53    True
52   1234.56    False
..       ...      ...
176      NaN    False
177      NaN    False
178      NaN    False
179  1920.15    False

正确的做法是什么?

python
  • 1 个回答
  • 75 Views
Martin Hope
Viktor
Asked: 2023-12-01 02:33:03 +0800 CST

在 pandas 中检查一个数据帧与另一个数据帧的值的快速方法

  • 7

我有两个数据框。df1:

                   Date     High      Mid      Low
1   2023-08-03 00:00:00  29249.8  29136.6  29152.3
4   2023-08-03 12:00:00  29395.8  29228.1  29105.0
10  2023-08-04 12:00:00  29305.2  29250.1  29137.1
13  2023-08-05 00:00:00  29099.9  29045.3  29073.0
18  2023-08-05 20:00:00  29061.6  29047.1  29044.0
..                  ...      ...      ...      ...
696 2023-11-26 20:00:00  37732.1  37469.9  37370.0
703 2023-11-28 00:00:00  37341.4  37138.2  37254.1
707 2023-11-28 16:00:00  38390.7  38137.2  37534.4
711 2023-11-29 08:00:00  38419.0  38136.3  38112.0
716 2023-11-30 04:00:00  38148.9  37800.1  38040.0

和 df2:

                Start      Top   Bottom
0 2023-11-28 00:00:00  37341.4  37138.2
1 2023-11-24 12:00:00  38432.9  37894.4

我需要检查第一个数据帧中的值是否落在第二个数据帧的行中的值的范围内,并将匹配数存储在列中。我可以使用这样的迭代来做到这一点:

for idx in df1.index:

    df2.loc[
        (df2.Start !=  df1.at[idx, 'Date']) &
        (df2.Bottom < df1.at[idx, 'High']) &
        (df2.Top > df1.loc[idx, ['Mid', 'Low']].max()),
        'Match'] += 1

但这种方式很慢。有没有更快的方法而不需要迭代?

python
  • 1 个回答
  • 42 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve