【问题标题】:Why the attribute of an instance model does not change为什么实例模型的属性不会改变
【发布时间】:2019-09-10 16:49:16
【问题描述】:

我创建了一个财务经理,我需要用户更改帐户活动(指示是否为活动帐户),但是当我发送更改表单时,模型属性不会更改并且始终保持 TRUE

我也尝试通过帐户副本来执行此操作,但这也不是结果的结果 Account.objects.get(user=self.request.user, id=id).is_active = False

models.py

class Account(models.Model):
    type_of_currency = models.CharField(max_length=20)
    user = models.ForeignKey(get_user_model(), blank=True,
                             related_name='user_account',
                             on_delete=models.CASCADE)
    count = models.DecimalField(max_digits=12, decimal_places=2, blank=True)
    created = models.DateTimeField(default=datetime.datetime.now)
    is_active = models.BooleanField()

    def __str__(self):
        return f'{self.type_of_currency} - {self.count}'

views.py

class AccountDetailView(DetailView, UpdateView):
    model = Account
    form_class = AccountCreateForm
    template_name = 'account_detail.html'

    def post(self, request, *args, **kwargs):
        id = self.request.POST['accountid']  
        self.request.user.user_account.get(id=6).is_active = False
        print(self.request.user.user_account.get(
            id=id).is_active)  # always True why?
        return redirect('/counts/' + id)

模板

{% extends 'base.html' %}
{% block content %}
    <div class="col-sm-5">
        <h1>account: {{ account.id }}</h1>

        <p><strong>Author:</strong> {{ account.user }}</p> <!-- author detail link not yet defined -->
        <p><strong>Type:</strong> {{ account.type_of_currency }}</p>
        <p><strong>count:</strong> {{ account.count }}</p>
        <p><strong>IsCreated:</strong> {{ account.created }}</p>
        <p><strong>IsActive:</strong>{{ account.is_active }}</p>
        <a class="btn btn-outline-primary"
           href="{% url 'account-list' %}">Back</a>
        {% if account.is_active %}
            <form method="post">
                {% csrf_token %}
                <input type="hidden" value="{{ account.id }}" name="accountid">
                <button type="submit" class="btn btn-outline-danger">Deactivate</button>
            </form>
        {% else %}
            <form method="post">
                {% csrf_token %}

                <button type="submit" class="btn btn-outline-success">Activate</button>
            </form>
        {% endif %}
    </div>
{% endblock %}

在 DetailViews 的 post 方法中,我期望按下按钮后,用户活动会发生变化,但结果始终为 True

【问题讨论】:

    标签: django postgresql django-views


    【解决方案1】:

    您永远不会对 DB 进行更改。

    另外,我不会使用以下语法:self.request.user.user_account.get(id=6).is_active = False

    你可以试试:self.request.user.user_acocount.filter(id=6).update(is_active=False)

    但是,如果你对使用.get() 感到死定

    user_acc = self.request.user.user_account.get(id=6)
    user_acc.is_active = False
    user_acc.save()
    

    【讨论】:

      猜你喜欢
      • 2016-05-27
      • 2013-11-08
      • 2022-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-15
      • 2011-03-12
      • 2019-02-06
      相关资源
      最近更新 更多